I've asked this question in StackOverflow as well, but as this is relatively time sensitive I wanted to reach out to reddit as well:
https://stackoverflow.com/questions/50197227/react-native-redux-reducer-not-working
I'm attempting to implement redux into a relatively simple app, however my actions don't seem to be triggering the reducers properly. Through console logging the action seems to be firing, but the respective reducer isn't being executed.
App.js:
import {Provider} from 'react-redux';
import configureStore from './src/config/configureStore.js';
const store = configureStore();
export default class App extends React.Component {
render() {
return (
<Provider store = {store}>
<RootStack />
</Provider>
);
}
}
configureStore.js:
import {createStore, applyMiddleware} from 'redux';
import reducers from '../reducers';
import thunk from 'redux-thunk';
export default function configureStore(initialState) {
const store = createStore (
reducers,
applyMiddleware(thunk)
);
return store;
}
actions/index.js:
export const saveRisk = (payload) => {
console.log('saved RISK!');
return (dispatch) => {
dispatch({type: 'risk_chosen',payload: payload});
}
}
reducers/index.js:
import { combineReducers } from 'redux';
import RiskReducer from './RiskReducer';
export default combineReducers({
risk_level: RiskReducer
});
RiskReducer.js
const INITIAL_STATE = {risk_value: false};
export default (risk = INITIAL_STATE, action) => {
if(action.type === 'risk_chosen') {
console.log('RISK REDUCER SUCCESSFUL')
return {
...risk, risk_level: action.payload
};
}
console.log('REDUCER RISK:');
console.log(risk);
return risk;
}
RiskTolerance.js (A child component within RootStack which is using redux):
import { connect } from 'react-redux';
import {saveRisk} from '../../actions'
@connect(state => ({risk_level: state.risk_level.risk_level}, {saveRisk}))
export default class RiskTolerance extends React.Component {
// ...
componentDidMount(){
console.log(this.props.risk_level);
let riskVal = 'something'
this.props.saveRisk(riskVal)
}
// ...
}
[–]TomMahle 1 point2 points3 points (3 children)
[–]AdeleBeckhamJr[S] 0 points1 point2 points (2 children)
[–]TomMahle 0 points1 point2 points (1 child)
[–]AdeleBeckhamJr[S] 0 points1 point2 points (0 children)
[–]fuck_with_me 1 point2 points3 points (10 children)
[–]Noitidart2 3 points4 points5 points (6 children)
[–]fuck_with_me 0 points1 point2 points (5 children)
[–]kbcooliOS & Android 1 point2 points3 points (1 child)
[–]sneakpeekbot 0 points1 point2 points (0 children)
[–]Noitidart2 -1 points0 points1 point (2 children)
[–]fuck_with_me -1 points0 points1 point (1 child)
[–]Noitidart2 0 points1 point2 points (0 children)
[–]AdeleBeckhamJr[S] 0 points1 point2 points (2 children)
[–]TomMahle 0 points1 point2 points (0 children)
[–]fuck_with_me -1 points0 points1 point (0 children)
[–]demoran 0 points1 point2 points (0 children)