Redux-saga报错patternOrChannel未定义,动态传参调用API求助
Hey there, let's break down why you're seeing this error and how to fix it quickly.
The Root Cause
That error pops up because the USER_SIGNIN pattern you're passing to takeEvery in your rootSaga is undefined. Looking at your action code, you defined the USER_SIGNIN constant but never exported it—so when your saga tries to import it, it gets nothing (undefined) instead of the actual action type string.
Step-by-Step Fix
Export all action type constants in your action file
You need to make sure every action type constant your reducer and saga depend on is exported so other files can access them. Update your action code like this:// Export all action type constants first export const CHECK_USER_LOGIN_STATUS = 'CHECK_USER_LOGIN_STATUS'; export const USER_SIGNIN = 'USER_SIGNIN'; export const USER_SIGNIN_RESULT = 'USER_SIGNIN_RESULT'; export const USER_SIGNIN_ERROR = 'USER_SIGNIN_ERROR'; // Then export your action creators export const userSignin = (username, password) => ({ type: USER_SIGNIN, username, password, }); export const checkUserLoginStatus = () => ({ type: CHECK_USER_LOGIN_STATUS, });Confirm your saga's imports are valid
Your existing import line is now correct with the constants exported, but just to verify:import { USER_SIGNIN, USER_SIGNIN_RESULT, USER_SIGNIN_ERROR } from '../actions/user';
Bonus: Fix a Potential Component Issue
In your handleLogin method, you're using username and password directly, but those variables aren't defined in that scope. Make sure you're pulling them from your component's state (or props) like this:
handleLogin = () => { const { username, password } = this.state; // Assuming you stored these in component state this.props.dispatch(userSignin(username, password)); };
That should resolve the saga error and get your login API call working as expected.
内容的提问来源于stack exchange,提问作者Mort

