关于本教程“React Router Native - Passing Data” https://www.youtube.com/watch?v=SdOWxoH3HLg @benawad user:4272160 我不知道如何
{"val1:"bob","val2":5}
从字符串中提取 bob 或 5<Text>JSON.stringify(location.state)}</Text>
在页面之间传递数据时的字符串数据
我试图通过评论联系@benawad,我在谷歌和这里搜索过类似的东西,但发现没有相关性。我尝试了一个正则表达式没有成功,但无论如何必须有更好的方法......
代码位于https://github.com/benawad/react-router-native-example/tree/1_advanced
// Home.js
import React from "react";
import { View, Text, Button } from "react-native";
export default ({ history }) => (
<View>
<Text>This is the home page</Text>
<Button title="change page" onPress={() =>
history.push("/products", {val1: "bob", val2: 5})
/>
</View>
);
// Products.js
import React from "react";
import { View, Text, Button } from "react-native";
export default ({ history, location }) => (
<View>
<Text>Product 1</Text>
<Text>Product 2</Text>
<Text>{JSON.stringify(location.state)}</Text>
<Button title="change page" onPress={() => history.push("/")}/>
</View>
);
我想过尝试 JSON.parse 字符串化的数据。没有喜悦。我试过 location.state.val 但刚刚得到
TypeError: undefined is not an object (evaluating 'location.state.val')