简体   繁体   English

组件中的 store.getState 或 mapStateToProps

[英]store.getState or mapStateToProps in Component

I have a question that what is the difference between use getState from store directly or use mapStateToProps.我有一个问题,直接从 store 使用 getState 或使用 mapStateToProps 有什么区别。 Please look at me example below请看我下面的例子

import React, { Component } from 'react'
import store from '../store'
import { connect } from 'react-redux';

class Test extends Component {
  constructor(props) {
    super(props);
  }

  render() {
    return (
      <p>
        <h1>{this.props.count}</h1>
        <h2>{store.getState().reducer1.count}</h2>
      </p>
    )
  }
}

const mapStateToProps = (state) => ({
  count: state.reducer1.count
});

// export default Test;
export default connect(mapStateToProps)(Test);

Both store.getState and mapStateToProps above work normally, it still updates when state change.上面的store.getStatemapStateToProps都正常工作,当状态改变时它仍然更新。 If we just use getState only, we don't need to use connect method.如果我们只使用 getState ,我们就不需要使用connect方法。

Another point I've recognized is when use mapStateToProps with connect , in reducer we must return a new copy of object state than return that state with modification.我认识到的另一点是当mapStateToPropsconnect一起使用时,在 reducer 中,我们必须返回对象状态的新副本,而不是通过修改返回该状态。 If not, component will not update when state changed.如果没有,组件将不会在状态改变时更新。 Like this:像这样:

return Object.assign({}, state, {
        count: state.count + 1,
        payload: action.payload,
      });

But if we use store.getState() , we can either return a new copy or the revised one.但是如果我们使用store.getState() ,我们可以返回一个新副本或修改后的副本。 Like this:像这样:

state.count++;
state.payload = action.payload;
return state

Anyone know please explain to me, thank you.有知道的请解释一下,谢谢。

P/S: and similar with store.dispatch vs mapDispatchToProps, those 2 will work normally, just want to know why we should use mapToProps with connect instead of call the function directly from the store. P/S: 和 store.dispatch vs mapDispatchToProps 类似,这两个都可以正常工作,只是想知道为什么我们应该使用 mapToProps 和 connect 而不是直接从 store 调用函数。

mapStateToProps is just a helper function which is really helpful to manage the project in modular style. mapStateToProps只是一个辅助函数,它非常有助于以模块化方式管理项目。 For example, you can even place all the logic of connect in separate files and use where you want.例如,您甚至可以将连接的所有逻辑放在单独的文件中并在您想要的地方使用。

Suppose if you're working on a large scale application, then guess a sorts of properties nested there.假设您正在开发一个大型应用程序,然后猜测嵌套在那里的各种属性。 Using connect you're actually modularizing project which is very helpful for developers who watch the project.使用 connect 您实际上是在模块化项目,这对观看项目的开发人员非常有帮助。

If you don't, you're writing several lines of code in single file.如果不这样做,您就是在单个文件中编写多行代码。


A possible problem you'll face when using getState() or dispatch() directly.直接使用 getState() 或 dispatch() 时可能会遇到的问题。 See this post for a little help to make it clear.请参阅此帖子以获取一些帮助以使其清楚。


The key benefit using connect is that you don't need to worry about when state is changed using store.subscribe(), the connect will let you know each state change whenever it gets updates.使用 connect 的主要好处是您无需担心使用 store.subscribe() 何时更改状态,connect 会在每次更新时通知您每个状态更改。

Also, react core concept is based on props and states.此外,react 的核心概念是基于 props 和 state。 Using connect allows you to get redux state as props.使用 connect 可以让你获得 redux state 作为 props。 Using this.props :)使用this.props :)

And ah, I remembered at what condition I accessed the store directly rather than using connect.啊,我记得在什么情况下我直接访问商店而不是使用连接。 In my project, I needed to save all the redux state in different form to somewhere and I din't need to connect it to any component.在我的项目中,我需要以不同的形式将所有 redux 状态保存到某个地方,我不需要将它连接到任何组件。 In this case, direct usage with redux store is very easy and helpful.在这种情况下,直接使用 redux store 是非常容易和有用的。 But if we try the same with connect in this case, then we'll have a difficult time.但是,如果我们在这种情况下对 connect 尝试相同的方法,那么我们将遇到困难。

Thus, I would suggest you to use them in separate condition.因此,我建议您在单独的条件下使用它们。

  1. Use connect if you want to map with component.如果要与组件映射,请使用连接。
  2. Access redux store directly if you don't need to map with component.如果不需要使用组件映射,直接访问redux store。

Further, this blog will explain a bit more: react redux connect explained此外,这个博客将解释更多: react redux connect 解释

Redux Flow: Redux 流程:

在此处输入图片说明

Using connect with react component:使用 connect 与 react 组件:

在此处输入图片说明

To conclude: Using connect, you use the provider and it lets the every child component to access the store by providing a provider and using store props in root app component.总结:使用connect,你使用提供者,它让每个子组件通过提供一个提供者并在根应用程序组件中使用商店道具来访问商店。

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

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