【问题标题】:React Native Redux dispatch not working but with no errorReact Native Redux 调度不起作用但没有错误
【发布时间】:2020-10-26 00:11:40
【问题描述】:

我似乎无法弄清楚为什么我无法更新计数器,即使我正在调度正确的操作类型并且它们属于 INCREMENT 和 DECREMENT 状态。我尝试传递一个 mapDispatchToProps 并将函数放入该函数中,但我仍然遇到同样的问题,由于某种原因没有更新状态。

索引:

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

应用程序

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

计数器

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

【问题讨论】:

    标签: javascript reactjs react-native redux


    【解决方案1】:

    这是 React Native 应用程序,对吧?您的错误是您使用onClick 代替Button 而不是onPressIt works fine now 切换该道具名称后。

    【讨论】:

      【解决方案2】:

      需要注意以下几点才能使其正常工作:

      1. 将道具 onClick 改为 onPress
      2. 在 App 组件中创建一个构造函数,将“this”绑定到递增、递减动作创建函数。像这样:
      constructor() {
          super(props)
          this.increment = this.increment.bind(this);
          this.decrement = this.decrement.bind(this);
      }
      
      1. 最后,连接到 Redux 存储,并在 AlphaDevs 的答案中调度类似的操作

      【讨论】:

        【解决方案3】:

        你必须使用connect函数将App组件连接到redux store。

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

        【讨论】:

        • 我已经连接了两者,但我是按照我的风格写的,所以我没有复制/粘贴它。不幸的是,仍然没有做任何事情。
        • @azimov308 你检查按钮点击事件是否正确调用了吗?
        • 在增量函数中我将 this.props.increment() 更改为 this.props.dispatch({type: 'INCREMENT'});它仍然不起作用。 Dispatch 收到正确的操作,但没有正确处理。
        • @azimov308 我认为按钮单击操作不适用于您的代码,正如我之前提到的那样。
        • 问题是我使用了onClick而不是onPress,谢谢!
        猜你喜欢
        • 2020-08-18
        • 1970-01-01
        • 1970-01-01
        • 2017-08-28
        • 2018-09-03
        • 2019-08-13
        • 1970-01-01
        • 2018-05-30
        • 1970-01-01
        相关资源
        最近更新 更多