简体   繁体   English

MapStateToProps 到同步上下文中的组件状态

[英]MapStateToProps to component state in an sync context

I know we can easily send the content of mapStateToProps in the component's state by doing so :我知道我们可以通过这样做轻松地在组件的状态下发送mapStateToProps的内容:

constructor(props){
  super(props);

  this.state = {
     filteredApps: this.props.apps
  }
}

In this usecase, this.state.filteredApps gets filled with what was mapped to props from Redux.在这个用例中, this.state.filteredApps填充了从 Redux 映射到 props 的内容。

But what if this.props.apps is only filled properly after an async call?但是如果this.props.apps仅在异步调用后才被正确填充怎么办? In an async context, this.props.apps will probably be an empty array for when it is initialized until the real data is fetched.在异步上下文中, this.props.apps在初始化时可能是一个空数组,直到获取实际数据。 Take this as an example :以此为例:

class AppFilterer extends React.Component {

  constructor(props) {
    super(props);

    this.state = {
      filteredApps : this.props.apps
    }
  }

  componentWillMount() {
      this.props.getApps();
  }

  render(){ return <div> </div> }

}

const mapStateToProps = state => {
   let { apps } = state.Admin;
   return { apps };
};

export default connect(mapStateToProps, { getApps })(AppFilterer);

In this case, my Redux action (which is caught by an Saga) this.props.getApps();在这种情况下,我的 Redux 操作(被 Saga 捕获) this.props.getApps(); is the call that fills my props full of apps and is called from the componentWillMount function.是将我的props填满应用程序的调用,它是从componentWillMount函数调用的。 It is initialized as an empty array and then gets filled with apps once the call is complete.它被初始化为一个空数组,然后在调用完成后填充应用程序。

I wish to filter these apps once they are fetched from the API so want to put them inside my component's state so that I don't mess with the Redux state.我希望在从 API 获取这些应用程序后对其进行过滤,因此希望将它们放入我的组件状态中,这样我就不会弄乱 Redux 状态。 What is the best practice for updating the component's state in this case?在这种情况下更新组件状态的最佳实践是什么? In other words, is there any way to take the result of a saga that has been mapped to props and set it into the component's state or am I looking for a weird pattern and should filter it some other way?换句话说,有没有办法获取已映射到 props 的 saga 的结果并将其设置为组件的状态,或者我是否正在寻找一种奇怪的模式并应该以其他方式对其进行过滤?

First of all API calls go in componentDidMount not in componentWillMount which is also now deprecated.首先,API 调用在componentDidMount而不是在componentWillMount ,它现在也已弃用。 Please refer this guide:请参考本指南:

https://reactjs.org/docs/react-component.html https://reactjs.org/docs/react-component.html

Secondly, when you are using redux state and mapping it to props, you should not set that in your component local state, that's not a good practice.其次,当你使用 redux state 并将其映射到 props 时,你不应该在你的组件本地 state 中设置它,这不是一个好的做法。 You'll receive updated props when your promise will return and you can always rely on props in that scenario.当您的承诺返回时,您将收到更新的道具,并且在这种情况下您始终可以依赖道具。

But if you still want to do that you can override componentDidUpdate(prevProps) which will be called when your props or state is updated.但是如果你仍然想这样做,你可以覆盖componentDidUpdate(prevProps) ,当你的 props 或 state 更新时,它会被调用。 Here is where you can set your state if you still want to do that.如果您仍然想这样做,可以在此处设置状态。

Note for your filter thing注意你的过滤器

You can do filtering in componentDidUpdate method like: this.setState({filteredApps. this.props.apps.filter(<your filter logic>)})您可以在componentDidUpdate方法中进行过滤,例如: this.setState({filteredApps. this.props.apps.filter(<your filter logic>)})

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

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