I am getting an error in my console,
my JSON is here https://dev.justinblayney.com/wp-content/uploads/2020/12/main-menu.json_.zip
Warning: Each child in a list should have a unique "key" prop.
What displays on my page is (So they are all unique, so WTF react)
KEY: 2429 KEY: 2430 KEY: 3859 KEY: 2421 KEY: 2802 KEY: 2428
On a side note, I'm discovering that using a function is a terrible way to get a JSON file, I also get memory leak warnings and every tutorial I see online uses classes or axios
Check the render method of MyRoutes
. See https://reactjs.org/link/warning-keys for more information.
MyRoutes@http://localhost:3000/react-wordpress-headless/static/js/main.chunk.js:63:81
div
Router@http://localhost:3000/react-wordpress-headless/static/js/0.chunk.js:35747:30
BrowserRouter@http://localhost:3000/react-wordpress-headless/static/js/0.chunk.js:35367:35
App@http://localhost:3000/react-wordpress-headless/static/js/main.chunk.js:94:1
function MyRoutes() {
const [myrt, setMyrt] = useState([]);
useEffect(() => {
fetch("main-menu.json" ,{
headers : {
'Content-Type': 'application/json',
'Accept': 'application/json'
}
})
.then(res => res.json())
.then(json =>{
setMyrt(json.items)}
)
});
return (
<>
{Object.keys(myrt).map((ky, idx)=> (
<>
<h2>KEY: {myrt[ky].ID} </h2>
<Route exact path={`/${myrt[ky].slug}`} component={Page} key={myrt[ky].ID} /></>
))}
</>
);
}
key
isn't on the actual child, which is the fragment. – jonrsharpe