Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs Redux-从函数调用操作_Reactjs_React Native_Redux_React Redux - Fatal编程技术网

Reactjs Redux-从函数调用操作

Reactjs Redux-从函数调用操作,reactjs,react-native,redux,react-redux,Reactjs,React Native,Redux,React Redux,我正在尝试从函数调用redux操作。我从中调用函数的组件已经连接到存储。但是,如果我通过以下操作,则它不起作用: function myFunc(action) { action() } 有没有办法通过参数传递动作?谢谢。使用redux的bindActionCreator是从组件分派操作的最简单方法 import { connect } from 'react-redux'; import { bindActionCreators } from 'redux'; class Redu

我正在尝试从函数调用redux操作。我从中调用函数的组件已经连接到存储。但是,如果我通过以下操作,则它不起作用:

function myFunc(action) {
    action()
}

有没有办法通过参数传递动作?谢谢。

使用redux的
bindActionCreator
是从组件分派操作的最简单方法

import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';

class ReduxConnectedComponent extends React.Component {
 mainFunction = () =>{
  this.props.someAction(); // this dispatches your action.
  }
 render(){}
}
const mapStateToProps = store => ({
  test: store.modules.someObject,
});

const mapDispatchToProps = dispatch => bindActionCreators({
  someAction: yourActionCreatorFunction,
}, dispatch);
export default connect(maStateToProps,mapDispatchToProps)(ReduxConnectedComponent)

在mapDispatchToProps中,我们使用
bindActionCreator
将函数绑定到
redux
存储
调度
。所以无论你什么时候叫它。它实际上调度存储操作

该操作必须连接到调度,以便reducer捕获它并更新存储

为此,您应该将您的操作作为redux的connect函数的
mapDispatchToProps
arg。它看起来是这样的:

connect(null,{actionCreator})(MyComponent)

要将连接的action creator从组件内传递给函数,请通过props:
myFunc(this.props.actionCreator)

总而言之:

import myFunc ...
class MyComponent extends React.Component {

  onChange() {
    myFunc(this.props.actionCreator)
  }

  render() { ... }
}

export connect(null, { actionCreator })(MyComponent)

现在,当
myFunc
执行
actionCreator()
时,它将正确地调度要由减速器捕获的动作。

更新的2021方法

您现在可以使用hook来分派任何操作 第一次导入
useDispatch
from
react redux
时,如下图所示

import { useDispatch } from 'react-redux';
const dispatch = useDispatch();
现在在功能组件内部创建一个变量,如bellow

import { useDispatch } from 'react-redux';
const dispatch = useDispatch();
现在,从任何地方发送/调用任何类似贝娄的操作

dispatch(action());

您需要将函数分派到redux存储区。因为它不是一个完整的代码,所以很难根据它给出建议。如果您在
React.Component
中使用它,使用带有mapActionToProps的“React-redux”连接功能,您可以访问React-props中的操作。