弹出

时间:2016-12-30 02:57:16

标签: listview react-native navigator

我有导航器的问题...我有2个场景,主场景包含一个ListView,第二个场景包含一个选择列表,允许我过滤主场景的ListView元素...问题是当我从第二个场景返回时,未绘制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>
    );
  }
}

1 个答案:

答案 0 :(得分:0)

可能是您忘了添加参考号。

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