使用React-Redux,状态的prop是否覆盖父对象的prop?

问题描述 投票:-1回答:1

使用React + Redux时,父道具会从状态覆盖道具,还是状态道具会覆盖父组件的道具(如果道具名称发生冲突,这是吗?

interface OwnProps {
  //The type for the props provided by the parent component
}

function mapDispatch(dispatch: AppDispatch<any>) {
  return { dispatch };
}

function mapState(state: RootState) {
  return {
    s: {
      // props from state are namespaced onto s
    }
  };
}

type StateProps = ReturnType<typeof mapState>;
type DispatchProps = ReturnType<typeof mapDispatch>;
type AllProps = StateProps & DispatchProps & OwnProps;

const ResetPassword: React.FC<AllProps> = (p) => {
  ///
}
reactjs redux react-redux
1个回答
0
投票
const Component = ({ testProp }) => (
  <div>testProp: {testProp}</div>
);

const mStP = () => ({
  testProp: 'testProp from mapStateToProps',
});

const ConnectedComponent = connect(mStP)(Component);

然后在您的App组件中的某个位置执行此操作:

<ConnectedComponent testProp="testProp from parent component" />

如您所见,mapStateToProps决定testProp的值。如果要保留父组件传递的值,只需不要将testProp放在mapStateToProps返回的对象中。

[如果您不使用ownProps中的第二个mapStateToProps参数,则传递给ConnectedComponent的所有道具仍会在那里-Redux不会干扰默认的React行为。

© www.soinside.com 2019 - 2024. All rights reserved.