【问题标题】:React navigation params doesn't reset?反应导航参数不会重置?
【发布时间】:2019-02-28 11:45:39
【问题描述】:

我有过滤屏幕和搜索屏幕。当用户从搜索屏幕导航时,我将搜索词从搜索屏幕传递到主屏幕。同样,我正在为过滤屏幕做同样的事情(将 filterOptions 从过滤屏幕传递到主屏幕)。

现在,当我从过滤屏幕转到主屏幕时,我想将 searchTerm 设置为 null,类似地,如果我从搜索屏幕转到主屏幕,我想将 filterOptions 设置为 null,我该怎么做?

在下面的代码中,A 行和 B 行没有将 filterOptions 和 searchTerm 设置为 null,这就是为什么过滤器和搜索不起作用,因为当 filterOptions 不为空时,我希望 searchTerm 为空,这意味着只有过滤器才能工作在那种情况下。同样,如果 filterOptions 为空,则 searchTerm 不能为空,这意味着只有搜索才能在该实例上工作。

代码:

filterProjectResults = (projects) => {

      let filterOptions = this.props.navigation.getParam('filterOptions', null);
      let searchTerm = this.props.navigation.getParam('searchTerm', null);

      console.log('Initial filterOptions ', filterOptions);  --> initially null
      console.log('intital searchTerm ', searchTerm); --> initially null


      if(filterOptions !== null){ 
        console.log('Inisde filter screen ', filterOptions)
        let display_filtered_projects = projects.filter((item) => item.floorplans.some(val => filterOptions.includes(val.bhk)));
        searchTerm = null;  <--- Line A
        return display_filtered_projects;

      } else if (searchTerm !== null) {
        console.log('Inside Search screen ', searchTerm);
        let search_filter_projects = projects.filter(
          (project) => {
            return project.name.toLowerCase().indexOf(searchTerm.toLowerCase()) !== -1
          }
        );
        filterOptions = null;   <<--- Line B
        return search_filter_projects;

      }

  }

search.js:

<TouchableOpacity onPress={() => this.props.navigation.navigate('Projects', {searchTerm : this.state.text})}>
    <Image source={require('../../assets/images/search.png')} style={{width: 24, height: 24, resizeMode: 'contain'}}/>
</TouchableOpacity>

filter.js:

<TouchableOpacity onPress={() => this.props.navigation.navigate('Projects', {filterOptions: this.state.filterOptions})}>
    <View style={styles.filterButton}>
        <Text style={styles.filterButtonText}>Apply Filters</Text>
    </View>
</TouchableOpacity>

我在 Flatlist 中传递这个过滤或搜索的数据以显示结果。如果我尝试使用状态来实现它,我会得到错误 -> https://imgur.com/a/P4vyRsu 代码:https://gist.github.com/aditodkar/1df0175e2b13727daaae0373040f8dcf

【问题讨论】:

  • 您在哪里存储 searchTerm 和 filterOptions?处于组件状态? A线和B线是什么?是变量还是状态?
  • @Dhaval 我没有使用状态,我将它存储在变量中。检查更新的问题。
  • 那么你应该使用状态。然后当过滤器在那个时候为搜索设置状态时,设置状态为 this.setState({ searchTerm: null}) ,反之亦然为 searchTerm。
  • @Dhaval 但这给了我最大状态更新超出错误。我在渲染中调用 filterProjectResults ()
  • @Dhaval 检查更新问题

标签: reactjs react-native react-navigation


【解决方案1】:

当你打电话时

this.props.navigation.navigate('Projects', {searchTerm : this.state.text})}&gt; 你也可以这样加filterOptions: null吗

this.props.navigation.navigate('Projects', {searchTerm : this.state.text, filterOptions: null})}?

然后你可以打电话给另一个人

this.props.navigation.navigate('Projects', {searchTerm : this.state.text, searchTerm: null})}

您也可以随时调用this.props.navigation.setParams(params) 并将其设置为null。

希望有帮助!

【讨论】:

    【解决方案2】:

    您可以在屏幕离开事件时使用导航生命周期on blur 更新参数,还必须指定您的参数,因为参数会进行浅层更新而不是覆盖值

    search.js:

    import {useFocusEffect} from '@react-navigation/native';
    import React, {useCallback} from 'react';
    
    useFocusEffect(
        useCallback(() => {
          return () => {
            navigation.setParams({ searchTerm: null });
          };
        }, []),
    );
    

    【讨论】:

    【解决方案3】:

    github上的这个问题issue#6674是同一个问题。

    这是默认行为。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-05
      • 1970-01-01
      • 2018-07-16
      • 1970-01-01
      • 1970-01-01
      • 2018-08-05
      • 1970-01-01
      • 2022-10-16
      相关资源
      最近更新 更多