首页 > 解决方案 > React Native 导航参数没有得到更新的值

问题描述

我有一个 React Native 应用程序,我从一个组件获取输入并将输入值作为导航参数发送到根据这些输入数据执行搜索的其他组件。现在,当我第一次navigation.getParam使用navigation.getParam. 这是我接受输入的地方:

inputData = () => {
    let bodyData = {
      customer_id: this.state.customerId,
      security_code: this.state.userSecurityCode,
      vendor: this.state.vendor,
      category_id: this.state.categoryName,
      country_id: this.state.countryName,
      zone_id: this.state.zoneName,
      navigationIdentity: 1
    }
      this.props.navigation.navigate('Vendor',bodyData)
  }

这是我收到的:

 componentWillMount() {
    const { navigation } = this.props;
    this.focusListener = navigation.addListener("didFocus", () => {
      const customer_id = navigation.getParam('customer_id')
      const security_code = navigation.getParam('security_code')
      const vendor = navigation.getParam('vendor')
      const category_id = navigation.getParam('category_id')
      const country_id = navigation.getParam('country_id')
      const zone_id = navigation.getParam('zone_id')
      let searchBody = {
        customer_id: customer_id,
        security_code: security_code,
        vendor: vendor,
        category_id: category_id,
        country_id: country_id,
        zone_id: zone_id
      }
      console.log('in focus',searchBody)
    });
  }

现在console.log这里只显示接收到的第一个输入值navigation.getParam。之后,当我更新输入值并导航时,它仍然显示第一个获取的值。

标签: react-nativereact-navigation

解决方案


希望这有效

 inputData = () => {
    let bodyData = {
      customer_id: this.state.customerId,
      security_code: this.state.userSecurityCode,
      vendor: this.state.vendor,
      category_id: this.state.categoryName,
      country_id: this.state.countryName,
      zone_id: this.state.zoneName,
      navigationIdentity: 1
    }
      this.props.navigation.push('Vendor',bodyData)
  }

 componentWillMount() {
    const { params } = this.props.navigation.state;
    this.focusListener = this.props.navigation.addListener("didFocus", () => {
      const customer_id = params.customer_id;
      const security_code = params.security_code;
      const vendor = params.vendor;
      const category_id = params.category_id;
      const country_id = params.country_id;
      const zone_id = params.zone_id;
      let searchBody = {
        customer_id: customer_id,
        security_code: security_code,
        vendor: vendor,
        category_id: category_id,
        country_id: country_id,
        zone_id: zone_id
      }
      console.log('in focus',searchBody)
    });
  }


推荐阅读