在React Native + Redux中使用Navigator

时间:2016-05-06 16:56:16

标签: javascript react-native react-redux

我是React Native的新手。 我有一个使用Redux框架的React Native应用程序。我使用Navigator组件来处理导航,但是在组件中未定义this.props.navigator。我怎样才能通过" this.props.navigator"组件? 我无法找到正确的方法,所以我正在寻求帮助和澄清。

index.anroid.js

...
import App from './app/app';

AppRegistry.registerComponent('RnDemo', () => App);

app.js

...    
import App from './containers/app';

const logger = createLogger();
const createStoreWithMiddleware = applyMiddleware(thunk, logger)(createStore);
const store = createStoreWithMiddleware(rootReducer);

const rootApp = () => {
  return (
    <Provider store={store}>
      <App />
    </Provider>
  )
}

export default rootApp;

容器/ app.js

...
class App extends Component {

  renderScene(route, navigator) {
    let Component = route.component
    return (
      <Component navigator={navigator} route={route} />
    )
  }

  configureScene(route) {
    if (route.name && route.name === 'Home') {
      return Navigator.SceneConfigs.FadeAndroid
    } else {
      return Navigator.SceneConfigs.FloatFromBottomAndroid
    }
  }

  _selectPage(page) {
    if(page === 'detail') {
      this.props.navigator.push({
        component: Detail,
        name: 'Detail'
      }) // **this.props.navigator.push is not a function**
    }
  }
  render() {
    const { navigator } = this.props;
    console.log(navigator);  // **>>>>>> Undefined**
    return (
      <View style={styles.container}>
        <View style={styles.menu}>
          <TouchableOpacity onPress={() => this._selectPage('detail')}><View style={styles.menuItem}><Text>Detail</Text></View></TouchableOpacity>
        </View>
        <View style={styles.content}>
          <Navigator
            ref='navigator'
            style={styles.navigator}
            configureScene={this.configureScene}
            renderScene={this.renderScene}
            initialRoute={{
              component: Home,
              name: 'Main'
            }}
          />
        </View>
      </View>
    )
  }
}

1 个答案:

答案 0 :(得分:1)

renderScene(route, navigator) {
    let Component = route.component
    return (
        // <Component navigator={navigator} route={route} />
        <Component {...this.props} navigator={navigator} route={route} />
    )
}

试试吧