react native navigator parent component is not drawn after pop

259 views Asked by At

I have a problem with Navigator ... I have 2 scenes, the main scene contains a ListView and the second scene contains a selection list that allows me to filter the elements of the ListView of the main scene ... the problem is that when I return from the second scene, the filtered elements of the ListView are not drawn ... the rows are displayed but they are empty

I know they are displayed because I've styled the rows with a different color and the color is shown correctly, do I have to force the rendering of the ListView component somehow? If yes, how should I do it?

Here are some parts of the code (I've removed the irrelevant parts):

class App extends Component {
  renderScene(route, navigator) {
    switch (route.id) {
      case 'Main':
        return (<Main navigator={navigator} />);
      case 'FilterByBrand':
        return (<FilterByBrand navigator={navigator} brands={route.brands} callback={route.callback}/>);
    }
  }

  render () {
    return (
      <Navigator
        initialRoute={{id: 'Main'}}
        renderScene={this.renderScene}
      />
    );
  }
}

--

class Main extends Component {
  // code removed (irrelevant)

  updateBrands(brands) {
    var filter = {};

    // code to generate filter to be used not shown, is working ok

    this.setState({ brandFilter: filter });
  }

  render () {
    return (
      <Container>
        <Header>
          <Button>
            <Icon name='md-pricetags'
              onPress={()=>this.props.navigator.push({
                id: 'FilterByBrand',
                brands: this.state.brands,
                callback: this.updateBrands}
              )}
            />
          </Button>
        </Header>
        <Content>
          <ProdList
            item={this.state.items}
            brandFilter={this.state.brandFilter}
          />
        </Content>
      </Container>
    );
  }
}

--

class ProdList extends Component {
  constructor(props) {
    super(props);

    this.dataSource = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
    this._filter = this._filter.bind(this);
    this._genRow = this._genRow.bind(this);
  }

  _genRow(item) {
    // code not shown (irrelevant)
  }

  _filter() {
    let output = this.props.items;

    // filtering code not shown, working ok
    return output;
  }

  render() {
    var dataSource = this.dataSource.cloneWithRows(this._filter());
    return (
      <ListView
        style={styles.container}
        dataSource={dataSource}
        renderRow={this._genRow}
        enableEmptySections={true}
      />
    );
  }
}

--

class FilterByBrand extends Component {

  selectBrand(id) {
    brands = // save here the selected brands

    // callback
    this.props.callback(brands);

    this.setState({brands: brands});
  }

  render () {
    return (
      <Container>
        <Header>
          <Button transparent onPress={()=>this.props.navigator.pop()}>
            <Icon name='ios-arrow-back' />
          </Button>
          <Title>Select brand(s)</Title>
        </Header>
        <Content>
          <List
            dataArray={this.state.brands}
            renderRow={ (brand) =>
              <ListItem onPress={() => this.selectBrand(brand.id)}>
                <Text>{brand.name}</Text>
              </ListItem>
            }
          />
        </Content>
      </Container>
    );
  }
}
1

There are 1 answers

1
Shailesh Prajapati On

may be you forget to add ref.

var navigator;

class App extends Component {
  renderScene(route, navigator) {
    switch (route.id) {
      case 'Main':
        return (<Main navigator={navigator} />);
      case 'FilterByBrand':
        return (<FilterByBrand navigator={navigator} brands={route.brands} callback={route.callback}/>);
    }
  }

  render () {
    return (
      <Navigator
        ref={(nav) => { navigator = nav; }}
        initialRoute={{id: 'Main'}}
        renderScene={this.renderScene}
      />
    );
  }
}