-1
I have this saga that performs an http request and performs a Dispatch on my Reducer:
// worker Saga: will be fired on GET_FILIAIS_REQUEST actions
function* fetchFiliais(action) {
try {
const data = yield call(LoginServices.fetchFiliaisApi);
yield put(allActions.loginActions.receiveFilialData(data));
} catch (e) {
yield put({type: 'FETCH_FILIAIS_FAILED', message: e.message})
}
}
/*
Starts fetchFiliais on each dispatched `GET_FILIAIS_REQUEST` action.
*/
export default function* loginSaga() {
yield takeEvery("GET_FILIAIS_REQUEST", fetchFiliais);
}
In my login component I perform a Dispatch to call this saga:
const Login = props => {
const dispatch = useDispatch()
const filiais = useSelector(state => state.filiais)
useEffect(() => {
dispatch({type: 'GET_FILIAIS_REQUEST'})
setTimeout(function(){ console.log(filiais); }, 5000);
}, [dispatch, filiais])
This is my Return. In my console.log() you are correctly printing the type and payload, including printing "enter correct case":
const currentUser = (state = {}, action) => {
console.log(action.type)
console.log(action.payload)
switch(action.type){
case "SET_USER":
return {
...state,
user: action.payload,
loggedIn: true
}
case "LOG_OUT":
return {
...state,
user: {},
loggedIn: false
}
case "RECEIVE_FILIAL_DATA":
console.log('enter correct case')
return {
...state,
filiais: action.payload
}
default:
return state
}
But my console.log() that I put in the branch variable is printing Undefined. Because?
This is my index.js:
import loginSaga from './pages/login/login-form/saga'
// create the saga middleware
const sagaMiddleware = createSagaMiddleware()
const store = createStore(
rootReducer,
applyMiddleware(sagaMiddleware)
)
// then run the saga
sagaMiddleware.run(loginSaga)
ReactDOM.render(
<Provider store={store}>
<Router>
<Route path="/" component={App} />
</Router>
</Provider>, document.getElementById('root'));
This is my root Reducer:
import currentUser from './login'
import { combineReducers } from 'redux'
const rootReducer = combineReducers({
currentUser
})
export default rootReducer
Try to replace your
const filiais = useSelector(state => state.filiais)
forconst filiais = useSelector(state => state.currentUser.filiais)
and say what happens, please...– Felipe Avelar
@Felipeavelar printa the correct value however useEffect is called several times, if I take the variable Dispatch and branches of the second parameter of useEffect is called only once, however the value is not printed...
– veroneseComS