React Native Navigator Parent组件在POP之后未绘制



我对导航器有问题...我有2个场景,主场景包含一个listView,第二个场景包含一个选择列表,该列表使我能够过滤列表视图的元素主要场景...问题是,当我从第二个场景返回时,列表视图的过滤元素未绘制...行显示了,但它们是空的

我知道它们之所以会显示,是因为我已经用不同的颜色对行进行了样式,并且颜色正确显示,我是否必须以某种方式强制ListView组件的渲染?如果是,我应该怎么做?

这是代码的某些部分(我已删除了无关的部分):

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>
    );
  }
}

可能是您忘记添加参考

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}
      />
    );
  }
}

相关内容

  • 没有找到相关文章

最新更新