简体   繁体   English

在React-Redux中验证“容器组件”中的触发事件

[英]Validate Fired Events in 'Container Component' in React-Redux

I am trying to adopt the philosophy of performing all operations in 'Container Component' (aka smart component) & then just passing data on to the 'Presentation Components'. 我正在尝试采用在“容器组件”(又称智能组件)中执行所有操作,然后仅将数据传递到“演示组件”的理念。

I am stuck at this point, where I need to validate the user action (event fired) before I dispatch the action to the Reducer . 我被困在这一点上,在这里我需要在将操作分派给Reducer之前验证用户操作(触发事件)。 The way I want to do this is by validating the event in the function inside 'mapDispatchToProps'. 我要执行此操作的方法是通过验证“ mapDispatchToProps”内部函数中的事件。

The code looks like this: 代码如下:

const mapStateToProps = ({ oneState, twoState }) => {
    return({
        oneState : oneState,
        twoState : twoState
    });
};

 const mapDispatchToProps = ( dispatch ) => {
    return({
        dispatchOneAction : () => {
    // do the validation here. The validation requires access to 
    // the 'oneState' obj above in the 'mapStateToProps'
        }
    });
 };

const C_Element = connect( mapStateToProps, mapDispatchToProps )( Ele );

My question is, is it possible? 我的问题是,可能吗? Or must I perform the validation downstream in the presentation component and then call the 'dispatchOneAction' function? 还是必须在presentation component下游执行验证,然后调用“ dispatchOneAction”函数?

The connect allows a 3rd argument called mergeProps : connect允许第三个参数称为mergeProps

connect([mapStateToProps], [mapDispatchToProps], [mergeProps], [options]) connect([mapStateToProps],[mapDispatchToProps],[mergeProps],[options])

mergeProps is a function that will receive the result from your mapStateToProps , mapDispatchToProps and the props provided to your component. mergeProps是一个函数,将从您的mapStateToPropsmapDispatchToProps和提供给您的组件的mapDispatchToProps接收结果。 It allows you to use them all in order to manipulate and return the final props that should be applied to your component. 它允许您全部使用它们,以便操纵并返回应应用于组件的最终道具。 This could be an opportunity to decorate your action creators with additional validation logic based on your state. 这可能是一个机会,可以根据您的状态使用其他验证逻辑来​​装饰动作创建者。 You can do whatever you like, returning a completely new set of props to be applied to your component. 您可以做任何您想做的事情,返回一套全新的道具以应用于您的组件。

For example, using your described case: 例如,使用您描述的情况:

const mapStateToProps = ({ oneState, twoState }) => {
  return({
    oneState : oneState,
    twoState : twoState
  });
};

const mapDispatchToProps = ( dispatch ) => {
  return bindActionCreators({
    successAction: MyActions.successAction,
    failAction: MyActions.failAction
  }, dispatch);
};

const mergeProps = (stateProps, dispatchProps, ownProps) => {
  const { oneState, twoState } = stateProps;
  const { successAction, failAction } = dispatchProps;
  const validatorAction = () => {
     if (oneState && twoState) {
       successAction();
     } else {
       failAction();
     }
  } 

  return Object.assign(
    {},
    stateProps,
    // We are providing new actions props
    { validatorAction },
    ownProps        
  );
}

const C_Element = connect( mapStateToProps, mapDispatchToProps, mergeProps)( Ele);

Refer to the official react-redux docs for more info. 有关更多信息,请参考官方的react-redux文档


An alternative approach is to use redux-thunk based actions. 另一种方法是使用基于redux-thunk的操作。 This allows you to encapsulate logic within your action creators with access to the state. 这使您可以将逻辑封装在动作创建者中并可以访问状态。 You can additionally fire off further actions from within your thunk action. 你还可以断火从内采取进一步行动thunk行动。

For example: 例如:

function validatingAction() {
  return (dispatch, getState) => {
    const { stateOne, stateTwo } = getState();

    if (stateOne && stateTwo) {
      dispatch(successAction());
    }

    dispatch(failedAction());
};

One of the main benefits of separating "Containers" and "Presentational Components" is to handle all logic for a specific component inside it. 分离“容器”和“ Presentational组件”的主要好处之一是处理其中特定组件的所有逻辑。 That said, you might define an action that changes your state and only fire it when valid. 也就是说,您可以定义一个更改状态并仅在有效时才触发状态的操作。

Class PresentationalComponent extends React.Component {
  ...
  onEventHandler() {
    if ( eventIsValid ) this.props.changeState();
  }
  ...
}

And: 和:

Class ContainerComponent extends React.Component {
  ...
  render() {
    <PresentationalComponent changeState={actions.changeState} />
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM