0
How can i use a return value of a function in a UseEffect function in java script
How can i use a return value of a function inside a useEffect function in java script
1 Answer
+ 11
You should use the useState hook to save the function return value:
You can also use the useRef hook and set its current property.
function calculateValue() {
return 42;
}
const SomeComponent = (props) => {
const [value, setValue] = useState(null);
const refValue = useRef(null);
useEffect(() => {
const calculatedValue = calculateValue();
// approach 1
setValue(calculatedValue);
// approach 2
refValue.current = calculatedValue();
}, []);
return (
<>
<label>{value}</lavel>
<label>{refValue.current}</lavel>
</>
);
}
for more information:
Intro to React => Lifecycle Methods => The useEffect Hook
https://www.sololearn.com/Course/react/?ref=app