如何在导航参数中传递获取响应?

时间:2019-03-29 08:21:14

标签: react-native

在屏幕搜索中,我创建了一个提取操作,该操作返回一个json作为响应。 我将此响应设置为参数以导航至屏幕B。 创建此参数的最佳做法是什么?

我只是发送了响应,但在另一端则是未定义的!

fetch(this.endPoint, {
            method: "GET",
            headers: null,
        }).then(response => response.json())
            .then(response => {
                console.log('the result is');
                console.log(response);

                this.props.navigation.navigate("Available", {'searchResult' : response})
            })
// in screen B
render() {
        let list = this.props.navigation.getParam('searchResult');
        console.log(this.list); // returns undefined
        return (
            .....
        );
    }

预期结果:收到Json 实际结果:收到未定义的

2 个答案:

答案 0 :(得分:0)

list基本上是render()方法中的变量。因此,您可以直接调用它而无需实例对象this

console.log(list);

此外,由于您使用的是getParam,因此最好使用list的默认后备值调用它,以防undefined。例如,如果list应该是一个值数组,则

let list = this.props.navigation.getParam('searchResult', []);

答案 1 :(得分:0)

尝试一下:

fetch(this.endPoint, {
        method: "GET",
        headers: null,
    }).then(response => response.json())
    .then(response => {
        console.log('the result is');
        console.log(response);
        /* 1. Navigate to the Available route with params */
        this.props.navigation.navigate('Details', {
            searchResult: response,
        });
    })

并从“可用路线”中读取参数:

 const list = navigation.getParam('searchResult', 'some default value');
 console.log(list); // without this