Javascript 将变量值从一个React函数传递到另一个函数(在两个不同的文件之间)

Javascript 将变量值从一个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

我被困在一些基本的问题上,不知道我应该找什么。。。 我曾尝试使用道具,但失败了

我有两个文件

第一个文件Map.js


导出默认函数映射(){
返回(
);
}
第二个文件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>;
    

是否使用诸如Redux之类的状态管理?MapView不是CurrentPosition的子组件。因此,您不能直接将其本地状态传递给MapView。或者存储全局状态(Redux/flux),或使用localStorage或useContext传递此值。在使用这些组件的位置发布整个代码。然后我可以建议这些选项中的哪一个更好。THX获得快速答案!!!我的第二次尝试现在变成“等待…”当我在Map.js.com中输入console.log text时,这是您所期望的还是应该在那时更新?我期望的值是let text=JSON.stringify(location.coords.latitude);是否检查
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))
}