I can't seem to figure out why I can't update the counter even tho I'm dispatching the correct action type and they fall within the INCREMENT and DECREMENT states. I tried passing a mapDispatchToProps and putting functions within that function and I still get the same problem, nothing updates the state for some reason.
Index:
import {createStore} from 'redux';
import {Provider} from 'react-redux';
import {reducer} from './src/reducers/counter';
const store = createStore(reducer);
const Main = () => (
<Provider store={store}>
<App />
</Provider>
);
AppRegistry.registerComponent(appName, () => Main);
App
import {connect} from 'react-redux';
// create a component
class App extends Component {
increment = () => {
this.props.dispatch({type: 'INCREMENT'});
};
decrement = () => {
this.props.dispatch({type: 'DECREMENT'});
};
render() {
return (
<View style={styles.container}>
<Button onClick={this.increment} title={'Add 1'} />
<Text>Counter {this.props.count} </Text>
<Button onClick={this.decrement} title={'Subtract 1'} />
</View>
);
}
}
Counter
const initState = {
count: 1,
};
export const reducer = (state = initState, action) => {
switch (action.type) {
case 'INCREMENT':
return {
count: state.count + 1,
};
case 'DECREMENT':
return {
count: state.count - 1,
};
default:
return state;
}
};