React native redux dispatch action



我是反应 redux 的新手,我正在尝试从连接的组件调度一个动作,但我不知道为什么该动作无法到达化简器。

===

=============================================================================================================================================================================================================================================================================================================================

以下是我代码的一部分

应用.js

import React, {Component} from 'react';
import { createStore, applyMiddleware, combineReducers } from 'redux';
import { Provider } from 'react-redux';
import thunk from 'redux-thunk';
import * as reducers from '../reducers';
import CounterApp from './counterApp';
const createStoreWithMiddleware = applyMiddleware(thunk)(createStore);
const reducer = combineReducers(reducers);
const store = createStoreWithMiddleware(reducer);
export default class App extends Component {
  render() {
    return (
      <Provider store={store}>
        <CounterApp />
      </Provider>
    );
  }
}

计数器应用.js

class CounterApp extends Component {
  constructor(props) {
    super(props);
  }
  render() {
    const { state, actions } = this.props;
    return (
      <RequestScreen
        counter={state.count}
        {...actions} />
    );
  }
}
export default connect(state => ({
    state: state.counter
  }),
  (dispatch) => ({
    actions: bindActionCreators(counterActions, dispatch)
  })
)(CounterApp);

请求屏幕

export class RequestScreen extends Component {
  constructor(props){
    super(props);
  }
  componentDidMount() {
  } 
  render() {
    const { state, actions } = this.props;
    return (
      <View>
        <TouchableOpacity onPress={() => this.props.increment)}>
          <Text>up</Text>
        </TouchableOpacity>
        <Text>123</Text>
        <Text>{state.count}</Text>
      </View>
    );
  }
}
const mapStateToProps = state => ({
  state: state.counter
})
const mapDispatchToProps = (dispatch) => ({
  increment: () => { dispatch({ type: 'INCREMENT' }) },
  decrement: () => { dispatch({ type: 'DECREMENT' }) },
  reset: () => { dispatch({ type: 'RESET' }) },
})
export default connect(mapStateToProps, mapDispatchToProps)(RequestScreen)

我认为这只是这一行的一个小错误:

<TouchableOpacity onPress={() => this.props.increment)}>

您需要在处理程序中调用该increment函数:

<TouchableOpacity onPress={() => this.props.increment()}>

或者只是直接通过它

<TouchableOpacity onPress={this.props.increment}>

相关内容

  • 没有找到相关文章

最新更新