React Native导航参数未获取更新值

时间:2019-08-21 08:43:59

标签: react-native react-navigation

我有一个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接收的第一个输入值。之后,当我更新输入值并导航时,它仍然显示第一个提取的值。

2 个答案:

答案 0 :(得分:0)

希望这行得通

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

答案 1 :(得分:-2)

HomeScreen.js

%d

Screen2.js

this.props.navigation.navigate('Screen2',{ customer_id: 123,security_code:'000',vendor: "abc",category_id: 1,country_id: 1,zone_id: 12});