I have a simple app using react-native and redux:
import { createReducer, createActions } from 'reduxsauce';
import Immutable from 'seamless-immutable';
/* ------------- Types and Action Creators ------------- */
const { Types, Creators } = createActions({
setAnswer: ['answer'],
});
export const QuestionnaireTypes = Types;
export default Creators;
/* ------------- Initial State ------------- */
export const INITIAL_STATE = Immutable({
answers: [],
});
/* ------------- Selectors ------------- */
export const QuestionnaireSelectors = {
getCurrentAnswers: (state: any) => state.answers,
};
/* ------------- Reducers ------------- */
const setAnswer = (state, { answer }) => {
return ({
...state,
answers: [...state.answers, answer],
})
};
/* ------------- Hookup Reducers To Types ------------- */
export const reducer = createReducer(INITIAL_STATE, {
[Types.SET_ANSWER]: setAnswer,
});
then in my component I simply do
const handleSubmitPress = (data) => {
setAnswer({ question_id: currentQuestion.id, value: data.questionInput });
console.log(getCurrentAnswers)
};
but every time this handler runs the log shows the previous state (an array containing all the already added but not the one added just in the previous line)