【问题标题】:Need help on some concepts about ReactNative and Redux and Navigator在一些关于 ReactNative 和 Redux 和 Navigator 的概念上需要帮助
【发布时间】:2016-02-21 11:21:51
【问题描述】:

在这个场景中经过几次测试,我有一些我自己无法回答的问题,所以我请求帮助澄清我的概念。

  1. 导航器中的提供者与道具

设置 Navigator 并将 store 传递到 React Native 应用程序的不同场景有什么区别以及最佳方法是什么

export default class App extends Component {
  render () {
    return (
      <Provider store={store}>                        //<-- here
        <Navigator style={{flex: 1}}
        initialRoute={{ component: MainContainer }} //<-- here 

          renderScene={ (route, navigator) => {
            const Component = route.component;
            return (
              <View style={{flex: 1, marginTop:40}}>
                <Component navigator={navigator} route={route} {...route.passProps} />
              </View>
  ...

MainContainer 与 react-redux 中的 Component 连接 connect 函数以将 Props 和 Actions 传递给 props

是更好地访问上下文还是道具?

const store = createStoreWithMiddleware(reducer, initialState); //<-- here
export default class App extends Component {
render () {
  return (
    <Navigator style={{flex: 1}}
      initialRoute={{ component: MainComponent }}

      renderScene={ (route, navigator) => {
      const Component = route.component;
      return (
        <View style={{flex: 1, marginTop:40}}>
          <Component 
             navigator={navigator} 
             route={route} 
             {...route.passProps} 
             store={store}                      //<-- here
               />
       </View>
    ...
  1. 在组件场景中,(不包装为智能容器)如何设置 listener 以了解 redux 状态 的变化或让我绑定 组件状态 到 redux 状态。

Navigator 中推送newScene 时将state(Redux 存储)和actions 传递为passProps,然后在newScene 调度操作并正确执行,状态为更新,但是.. . 不会重新渲染场景。

我是否必须将状态组件绑定到 Redux 状态才能看到屏幕上反映的更改?

在这种情况下是否有任何关于最佳实践的示例?

  1. props vs connect 在同一个Scene

同一个Scene中,从上到下的组件,这是将redux state(不是说component state)作为包装传递的最佳方法'smart' container 中的一个组件,带有来自react-redux 的“connect”,或者将孔场景作为props 传递。

【问题讨论】:

  • 如果您下次可以将您的问题分成几个帖子,那将是很酷的,您很可能会得到更快的回复;)

标签: reactjs react-native redux navigator react-redux


【解决方案1】:
  1. 应该使用第一个解决方案,因为提供者应该是最外层的组件(这样下面的所有东西都可以正确连接)。
  2. 您可以在componentDidReceiveProps 上运行回调,或者(我更喜欢)简单地连接需要访问商店的组件。这正是 redux 的用途。不这样做的唯一原因是,如果您想将一个组件与另一个存储内容重用(真正的代表性组件)
  3. 这在很大程度上取决于应用程序和组件的深度,但总的来说,这完全没问题。您也可以将信息作为 props 传递,但随着应用程序变得越来越大,您可能必须传递大量 props,这可能会混淆组件的真正意图

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-05-08
    • 2011-08-25
    • 1970-01-01
    • 1970-01-01
    • 2021-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多