Reactjs 如何通过redux react js调用一个函数到另一个函数

Reactjs 如何通过redux react js调用一个函数到另一个函数,reactjs,redux,Reactjs,Redux,react redux可以在函数中调用可能的父组件在函数中调用子组件。 通过redux。 //parent coponent firstFun(){ this.props.otherFuc() } //child component otherFun(){ alert('okk') } 可能的副本; Redux仅用于状态管理。您可以在不使用redux的情况下实现这一点。在redux中,您不调用组件 您使用的动作标记为一个常量示例: export const ActionA

react redux可以在函数中调用可能的父组件在函数中调用子组件。 通过redux。

//parent coponent
firstFun(){
    this.props.otherFuc()
}

//child component 
otherFun(){
    alert('okk')
}
可能的副本;


Redux仅用于状态管理。您可以在不使用redux的情况下实现这一点。

在redux中,您不调用组件

您使用的动作标记为一个常量示例:

export const ActionAddSomething = payload => ({
  type: 'ADD_MSG_GROUP',
  payload
});
然后,通过标记,使用减速机完成操作:

export const ReducerGetSomething = (state=[], action) => {
  switch (action.type) {
        case GET_MSG_GROUP:
      return action.msg

        default:
                return state;
  }
};
然后,您可以访问所有组件的reducer结果,如下所示(组件从HOC商店链接,底部为connect):

从“React”导入React;
从'react redux'导入{connect};
类应用程序扩展了React.Component{
建造师(道具){
超级(道具);
此.state={
}
}
render(){
常数{
还原什么的,
}=这是道具
返回(
{ReducerSortGroupList}
);
}
}
常量mapStateToProps=状态=>({
ReducerGetSomething:state.ReducerPortGroupList
});
const mapDispatchToProps=调度=>({
});
导出默认连接(mapStateToProps、mapDispatchToProps)(应用程序);
您可以从您希望的组件向父级发送操作,如下所示:

import React from "react";
import { ActionAddSomething } from './actions_folder/ActionAddSomething
import { connect } from 'react-redux';


const dumbComponent = ({ ReducerSortGroupList }) => {

  return (
    <dumbComponent>
      <Button onClick={ActionAddSomething('Text sent to action')}>send something to action</Button>
      <div>{ReducerSortGroupList}</div>
    </dumbComponent>
  );
}

const mapStateToProps = state => ({

});

const mapDispatchToProps = dispatch => ({
  ActionAddSomething: payload => dispatch(ActionAddSomething(payload))
});

export default connect(mapStateToProps, mapDispatchToProps)(dumbComponent);
从“React”导入React;
从“./actions\u文件夹/ActionAddSomething”导入{ActionAddSomething}
从'react redux'导入{connect};
常量组件=({ReducerSortGroupList})=>{
返回(
采取行动
{ReducerSortGroupList}
);
}
常量mapStateToProps=状态=>({
});
const mapDispatchToProps=调度=>({
ActionAddSomething:payload=>dispatch(ActionAddSomething(payload))
});
导出默认连接(MapStateTrops、mapDispatchToProps)(默认组件);
重要提示:您注意到,我们从一个文件夹将操作导入组件,您应该在该文件夹中添加所有操作以获得最佳实践,但我们不需要导入reducer,因为它已经通过“connect”从HOC store导入

总之:您可以通过每个组件访问您的商店,操作(组件中的导入操作)也是如此

最佳实践是将您的容器(父容器)连接到您的孩子将收到的每个操作和还原器,这样您就不需要多次调用存储并获得一点性能改进

将动作或减速器传递给子对象,例如:

家长:

<Parent>
   <Child
      actionName={this.props.actionName}
      reducerName={this.props.reducerName}
   />
</Parent>

Child:

<Child>
  <div>{props.reducerName}</div>
  <button onClick={props.actionName('send whatever')}>send whatever</button>
</Child>

儿童:
{props.reducerName}
送什么都行
如果是困惑,那是正常的,不得不练习别无选择^^

<Parent>
   <Child
      actionName={this.props.actionName}
      reducerName={this.props.reducerName}
   />
</Parent>

Child:

<Child>
  <div>{props.reducerName}</div>
  <button onClick={props.actionName('send whatever')}>send whatever</button>
</Child>