【发布时间】:2021-11-05 13:53:42
【问题描述】:
我有一个主组件,我在其中导入了一个组件,所以我想将一些数据从该导入的组件传递到主组件并显示它。这是一个示例代码: 这是我的主要文件:
import React from "react";
import { View, Text, StyleSheet, TouchableOpacity, Image } from "react-native";
import TestComponent from "../components/TestComponent";
function TestScreen(props) {
return (
<View>
<TestComponent />
<Text>{location}</Text>
</View>
);
}
export default TestScreen;
这是导入的组件:
import React from "react";
import { View, Text, StyleSheet, TouchableOpacity, Image } from "react-native";
function TestComponent(props) {
const [location, setLocation] = useState("");
return (
<View>
<Text>Boiler</Text>
<TouchableOpacity>
<Text onPress={() => setLocation("Me")}>Click</Text>
</TouchableOpacity>
</View>
);
}
export default TestComponent;
现在我想从TestComponent 组件中获取location 钩子并将其用于TestScreen 组件,我该怎么做
【问题讨论】:
-
来自 MVC 世界,看来您的
Controller级别太低了
标签: javascript reactjs react-native react-hooks