我试图将变量“ locationProps”从一个函数传递到另一个函数。但是我不知道如何在方法中发送变量以及如何访问变量。
function Daily({ locationProps = 2, root }) {
const context = useContext(ThemeContext);
const localization = useCallback(() => {
if (root && cookies.get("location") !== undefined) {
return cookies.get("location");
}
return locationProps;
}, [locationProps, root]);
function dayData() {
getSpeadsheetUrl=() =>{
return config.prData[locationProps];
}
答案 0 :(得分:0)
可以通过函数传递参数的一种方法是使用闭包。进一步了解closures。
您的情况:
// dayData.js
function dayData(locationProps) {
const getSpeadsheetUrl= () =>{
return config.prData[locationProps];
}
return { getSpreadsheetUrl }
}
// daily.js
function Daily({ locationProps = 2, root }) {
const context = useContext(ThemeContext);
// Now you can call getSpreadsheetsUrl, like this: getSpreadsheetsUrl()
const { getSpreadsheetUrl } = dayData(locationProps);
const localization = useCallback(() => {
if (root && cookies.get("location") !== undefined) {
return cookies.get("location");
}
return locationProps;
}, [locationProps, root]);
}