How can i use a return value of a function in a UseEffect function in java script | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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

25th Sep 2020, 2:38 PM
Issah Ibrahim
Issah Ibrahim - avatar
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
26th Sep 2020, 5:10 PM
Burey
Burey - avatar