【发布时间】:2021-01-01 22:14:00
【问题描述】:
import React,{useEffect,useCallBack} from 'react';
ReactfunctionalComponent = ({className, DropDownValue, ...rest}) => {
console.log(DropDownValue); //will log the value
const getCustomers = useCallback(async ()=> {
console.log(DropDownValue) // will not log the value (undefined)
};
useEffect(() => {
console.log(DropDownvalue) // will not log the value (undefined)
getCustomers();
},[getCustomers]);
return (
<ListView/>
)
}
无法在回调函数中传递 DropDwonValue
可以在所有范围内访问该值,但不能在回调函数内访问,如果我使用方法错误或有任何替代用法可以使用下拉值,请向我建议。
DropdownValue 来自另一个组件。
如果您知道如何在组件渲染上触发函数,请建议,基本上,我需要在下拉值进入时进行API调用。
【问题讨论】:
-
发布你的整个代码。
-
useCallback 应该始终使用依赖项数组调用,否则它什么也不做。
标签: reactjs axios react-hooks react-functional-component