I want to implement componentDidMount in hooks that I want to do something for the first rendering ONLY, so in the useEffect, I set the dependency array as empty, but eslint has the warning that dependency is missing.
how to solve it?
import React, { useEffect, useState } from "react";
import ReactDOM from "react-dom";
const Child = ({ age }) => {
useEffect(() => {
console.log("simulate componentDidMount, age:", age);
}, []);
useEffect(() => {
console.log('simulate componentDidUpdate, age:', age)
}, [age])
return <div>age: {age}</div>;
};
const App = () => {
const [age, setAge] = useState(0);
const handleOnClick = e => {
e.preventDefault();
setAge(a => a + 1);
};
return (
<>
<Child age={age} />
<button onClick={handleOnClick}>INCREASE AGE</button>
</>
);
};
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);