I have a code similar to this (I know this code is stupid, but it's to get an example)
import { useState, useEffect } from "react";
const MyComponent = (props) => {
const { names, indexes, onIndexesChange } = props;
const [sortDir, setSortDir] = useState(1);
useEffect(() => {
const newIndexes = [0, 1, 2];
newIndexes.sort((i1, i2) => {
return sortDir * names[i1].localeCompare(names[i2]);
});
onIndexesChange(newIndexes);
}, [sortDir, onIndexesChange]);
return (
<p>
<button onClick={() => setSortDir(-sortDir)}>Click</button>
<br />
{indexes.map((index) => names[index])}
</p>
);
};
export default function App() {
const names = ["Newton", "Einstein", "Pascal"];
const [indexes, setIndexes] = useState([0, 1, 2]);
// in real code, indexes is shared between multiple components,
// which it is declared above and not in MyComponent
return (
<div className="App">
<MyComponent
names={names}
indexes={indexes}
onIndexesChange={setIndexes}
/>
</div>
);
}
The above throws an expected warning
React Hook useEffect has a missing dependency: 'names'. Either include it or remove the dependency array.
I could add names
to the array of dependencies but then I get an infinite loop using React 18.2.0 (I do not get one using React 18.0.0) since names is redefined on each render.
How can I get this working regardless of how names
is declared (e.g., as a state or as a const variable)?
Sandbox code: https://codesandbox.io/s/clever-breeze-p1nmx7?file=/src/App.js:199-233