Javascript 将变量值从一个React函数传递到另一个函数(在两个不同的文件之间)
我被困在一些基本的问题上,不知道我应该找什么。。。 我曾尝试使用道具,但失败了 我有两个文件 第一个文件Map.jsJavascript 将变量值从一个React函数传递到另一个函数(在两个不同的文件之间),javascript,reactjs,react-native,Javascript,Reactjs,React Native,我被困在一些基本的问题上,不知道我应该找什么。。。 我曾尝试使用道具,但失败了 我有两个文件 第一个文件Map.js 导出默认函数映射(){ 返回( ); } 第二个文件currentPosition.js import {MyContext} from "./somewhere" export default function CurrentPossition() { const [location, setLocation] = useState(null); con
导出默认函数映射(){
返回(
);
}
第二个文件currentPosition.js
import {MyContext} from "./somewhere"
export default function CurrentPossition() {
const [location, setLocation] = useState(null);
const [errorMsg, setErrorMsg] = useState(null);
const [copiedText] = useState('')
const {setText} = React.useContext(MyContext)
useEffect(() => {
(async () => {
if (Platform.OS === 'android' && !Constants.isDevice) {
setErrorMsg(
'Oops, this will not work on Snack in an Android emulator. Try it on your device!'
);
return;
}
let { status } = await Location.requestPermissionsAsync();
if (status !== 'granted') {
setErrorMsg('Permission to access location was denied');
return;
}
let location = await Location.getCurrentPositionAsync({});
setLocation(location);
})();
}, []);
if (errorMsg) {
setText(errorMsg);
} else if (location) {
setText(JSON.stringify(location.coords.latitude))
}
导出默认函数currentPosition(){
const[location,setLocation]=useState(null);
常量[errorMsg,setErrorMsg]=useState(null);
常量[copiedText]=useState(“”)
useffect(()=>{
(异步()=>{
if(Platform.OS==='android'&&!Constants.isDevice){
setErrorMsg(
“哎呀,这在Android模拟器中的零食上不起作用。请在您的设备上试用!”
);
返回;
}
让{status}=wait Location.requestPermissionsAsync();
如果(状态!=“已授予”){
setErrorMsg(“访问位置的权限被拒绝”);
返回;
}
let location=await location.getCurrentPositionAsync({});
设置位置(位置);
})();
}, []);
让text='Waiting..';
如果(错误消息){
text=errorMsg;
}else if(位置){
text=JSON.stringify(location.coords.latitude);
}
我想传递currentPosition.js变量的值let text
进入Map.js文件的
组件的纬度:
参数
我该怎么做
第二次尝试 我的Map.JS现在看起来像这样,但我没有成为正确的坐标。 当我记录文本的值时-我只是变得
等待…
这是来自
const[text,setText]=React.useState(“Waiting…”);
export const MyContext=React.createContext();
导出默认函数映射(){
const[text,setText]=React.useState(“等待…”);
console.log(文本);
返回(
);
}```
添加两个组件的公共父组件,并在子组件中访问它:
- 创建上下文并将其导出,以便子组件可以在需要时导入它
export const MyContext = React.createContext(); // Create state in the parent component const [text, setText] = React.useState("Waiting...");
- 在此提供程序中包装子项
<MyContext.Provider value={{ text, setText }}> // children </MyContext.Provider>;
setText
是否已到达并执行?因为如果触发,则应更新text
。当我尝试记录setText时,我变成[函数绑定的dispatchAction]
import {MyContext} from "./somewhere"
export default function CurrentPossition() {
const [location, setLocation] = useState(null);
const [errorMsg, setErrorMsg] = useState(null);
const [copiedText] = useState('')
const {setText} = React.useContext(MyContext)
useEffect(() => {
(async () => {
if (Platform.OS === 'android' && !Constants.isDevice) {
setErrorMsg(
'Oops, this will not work on Snack in an Android emulator. Try it on your device!'
);
return;
}
let { status } = await Location.requestPermissionsAsync();
if (status !== 'granted') {
setErrorMsg('Permission to access location was denied');
return;
}
let location = await Location.getCurrentPositionAsync({});
setLocation(location);
})();
}, []);
if (errorMsg) {
setText(errorMsg);
} else if (location) {
setText(JSON.stringify(location.coords.latitude))
}