“这是什么意思?”;选择SubReddit“;来自Redux Reddit API示例

“这是什么意思?”;选择SubReddit“;来自Redux Reddit API示例,redux,react-redux,Redux,React Redux,全部: 我对Redux非常陌生,当我了解它的RedditAPI示例时,有一个代码片段让我非常困惑: 在AsyncApp.js中,有: componentDidMount() { const { dispatch, selectedSubreddit } = this.props dispatch(fetchPostsIfNeeded(selectedSubreddit)) } 我想知道dispatch和selectedSubreddit在哪里绑定到这个.props 感谢该示

全部:

我对Redux非常陌生,当我了解它的RedditAPI示例时,有一个代码片段让我非常困惑:

在AsyncApp.js中,有:

componentDidMount() {
    const { dispatch, selectedSubreddit } = this.props
    dispatch(fetchPostsIfNeeded(selectedSubreddit))
  }
我想知道dispatch和selectedSubreddit在哪里绑定到这个.props


感谢该示例正在使用中的
connect()
函数注入Redux状态的某些部分以及商店的
dispatch()
函数作为该组件中的道具。有关更多信息,请参阅

例如:

App.js:

这里的
connect()
函数使用上面的
mapstatetops()
函数将Redux状态的适当部分作为props注入
组件中。
mapStateToProps()
返回的对象键对应于注入的道具的名称,对应的值是注入的道具的值

connect()
还可以接受第二个参数,
matchDispatchToProps()
,该参数可用于将特定的动作分派函数作为道具注入组件中。无论您是否向
connect()
提供任何参数,它都会将Redux商店的
dispatch()
函数作为名为
dispatch
的道具注入

这些连接的容器组件从应用商店接收状态更新,因此当您的Redux状态更改时,连接的容器组件将相应地接收新的道具

export class App extends Component {
  //...
}

function mapStateToProps(state) {
  const { selectedReddit, postsByReddit } = state
  const {
    isFetching,
    lastUpdated,
    items: posts
  } = postsByReddit[selectedReddit] || {
    isFetching: true,
    items: []
  }

  return {
    selectedReddit,
    posts,
    isFetching,
    lastUpdated
  }
}

export default connect(mapStateToProps)(App)