React-Redux-Firebase模板运行空白及reducer报错求助
Hey there, let's get this sorted out! That error message is spelling out exactly what's going wrong—your currentUser reducer is returning undefined when handling the FETCH_FIREBASE_USER action, which violates Redux's core rules: every reducer must always return a valid state value (even if you're ignoring the action entirely).
Here's how to fix it step by step:
- Find your
currentUserreducer file (it's usually located insrc/reducers/or a similar directory in the boilerplate). - Check its structure—you're either missing a case to handle
FETCH_FIREBASE_USER, or you don't have a default return statement that passes back the existing state for unhandled actions.
Example of what your broken reducer might look like:
const currentUser = (state = null, action) => { switch(action.type) { // Only handles other actions, no case for FETCH_FIREBASE_USER // No default return at the end } }
Updated working reducer:
const currentUser = (state = null, action) => { switch(action.type) { case 'FETCH_FIREBASE_USER': // Adjust this logic to match what the action is supposed to do— // if it's fetching user data from Firebase, return the payload or keep the current state if loading return action.payload || state; // Keep any existing case statements you already have here default: // Critical! Always return the current state for actions you don't explicitly handle return state; } }
Quick breakdown:
Redux expects every reducer to output a value on every action. When FETCH_FIREBASE_USER fires, your reducer wasn't returning anything (hence the undefined error). The default case ensures that any unhandled actions just pass through the existing state instead of crashing your app.
After making this change, restart your development server and check if the blank page is gone and the console error disappears. If you still run into issues loading the Firebase user, you might want to double-check the FETCH_FIREBASE_USER action creator to make sure it's correctly fetching and passing user data from Firebase.
内容的提问来源于stack exchange,提问作者chris kelly

