1

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;
  }
};

3 Answers 3

2

This is React Native app, right? Your error is that you used onClick for the Button instead of onPress. It works fine now after switching that prop name.

Sign up to request clarification or add additional context in comments.

Comments

0

These point need to be taken care of in order for this to work:

  1. Change the prop onClick to onPress
  2. Create a constructor in the App Component, to bind "this" to the increment, decrement action creator functions. Like this:
constructor() {
    super(props)
    this.increment = this.increment.bind(this);
    this.decrement = this.decrement.bind(this);
}
  1. Lastly, connect to Redux store, and dispatch the actions like ,in AlphaDevs's Answer

Comments

0

You have to connect the App component into the redux store with connect function.

import { connect } from 'react-redux';

// create a component
class App extends Component {
  increment = () => {
    this.props.increment();
  };

  decrement = () => {
    this.props.decrement();
  };

  render() {
    return (
      <View style={styles.container}>
        <Button onPress={this.increment} title={'Add 1'} />
        <Text>Counter {this.props.count} </Text>
        <Button onPress={this.decrement} title={'Subtract 1'} />
      </View>
    );
  }
}
const mapStateToProps = state => ({ count: state.count })
const mapDispatchToProps = dispatch => {
  return {
    increment: () => dispatch({ type: 'INCREMENT' }),
    decrement: () => dispatch({ type: 'DECREMENT' }),
  }
}
export default connect(mapStateToProps, mapDispatchToProps)(App);
    

6 Comments

I had connected both but I wrote it under my styles so I didn't copy/paste it over. Still doesn't do anything to count, unfortunately.
@azimov308 Did you check if the button click event called correctly?
in the increment function I changed this.props.increment() to this.props.dispatch({type: 'INCREMENT'}); and it still doesn't work. Dispatch receives the correct action but doesn't handle it correctly.
@azimov308 I think the button click action is not working on your code, as I mentioned before.
The problem was I used onClick instead of onPress, thanks!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.