理解redux和saga in native native

时间:2018-02-14 17:17:44

标签: react-native redux react-redux

我正在关注这个tutorial,我对redux,saga非常困惑......在教程的settings container中,他们使用以下代码来更改应用程序的语言。我很困惑,以前从未使用过redux,我不确定我是否完全理解以下代码。我用我能理解的代码评论了代码。你能告诉我这是否合适并帮助我更好地理解它?

 // map the store state to the component props, this will make to current laguage value availble in this.props.language? But why do they need to do this step? this.props.language is never called
const mapStateToProps = (state) => {
  return {
    language: state.settings.language,
  }
}

// create a saga called changeLanguage and once called will execute SettingsActions.changeLanguage asynchronously ?

const mapStateToDispatch = dispatch => ({
  changeLanguage: (newLang) => dispatch(SettingsActions.changeLanguage(newLang))
})

connect the saga to the store
export default connect(mapStateToProps, mapStateToDispatch)(SettingsContainer)

// ??
SettingsContainer.propTypes = {
  changeLanguage: PropTypes.func.isRequired,
  language: PropTypes.string.isRequired
}

1 个答案:

答案 0 :(得分:0)

根据我的理解,mapStateToProps方法将用于从reducer接收redux状态变量作为组件中的props,mapStateToDispatcher用于通过action.js将值传递给redux状态变量,然后将它传输到reducer.js这就像一个循环。 Redux商店将所有状态变量存储在一个名为One Store概念的地方 有关更多信息,请参阅此YouTube视频以获取Redux。我希望你能更好地理解,你的疑惑将得到澄清。要了解Redux,您应该具备React JS的基础知识,那么您将很容易理解Redux流程。

React JS / Redux教程:http://www.youtube.com/playlist?list=PL6gx4Cwl9DGBbSLZjvleMwldX8jGgXV6a