Skip to content

Passing props through Route #167

Closed
Closed
@lopezjurip

Description

@lopezjurip

I don't want to implement Flux/Redux, so I need to pass parameters through the Router. I'm trying this approach (in a nutshell):

import React, { Component, Navigator, StyleSheet, View, Text } from 'react-native'
import { Router, Route, Schema, Actions } from 'react-native-router-flux'
import Button from 'react-native-button'


export default class AppRouter extends Component {
  constructor(props) {
    super(props)
    this.state = { items: [0] }
  }

  render() {
    console.log('Item count on Router render:', this.state.items.length);
    return (
      <Router hideNavBar={false}>
        <Schema name="default" sceneConfig={Navigator.SceneConfigs.FloatFromRight}/>

        <Route name="main" schema="default" initial={true} component={Main}
               title="Main" rightTitle="Search" onRight={() => Actions.search()}
               items={this.state.items} />

        <Route name="search" schema="default" component={SearchView}
               onResult={(result) => this.setState(result)} />

      </Router>
    )
  }
}


class Main extends Component {
  constructor(props) {
    super(props)
    this.state = { items: this.props.items || [] }
  }

  componentWillReceiveProps(props) {
    console.log('Item count on componentWillReceiveProps:', props.items.length);
    this.setState({ items: props.items })
  }

  render() {
    console.log('Item count on Main render:', this.state.items.length);
    return (
      <View style={{ marginTop: 64 }}>
        {this.state.items.map((item, i) => <Text key={i}>{item}</Text>)}
      </View>
    )
  }
}


class SearchView extends Component {
  render() {
    return (
      <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
        <Button onPress={this.callback.bind(this)}>Close</Button>
      </View>
    )
  }
  callback() {
    Actions.pop()
    this.props.onResult({ items: [1, 2, 3] })
  }
}

It prints:

// App load
Item count on Router render: 1
Item count on Main render: 1
// Press 'Search' button
Item count on componentWillReceiveProps: 1
Item count on Main render: 1
// Press close button
Item count on Router render: 3
Item count on componentWillReceiveProps: 1 // did not pass items
Item count on Main render: 1
Item count on componentWillReceiveProps: 1
Item count on Main render: 1

Am I doing something wrong?

image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions