On updating data prop, app crashes with error "Cannot read property '0' of undefined"
See original GitHub issueWhen updating the data prop of the crossword the, the app crashes and return error Cannot read property '0' of undefined
.
Why I need to update data props ? I want to update the clue with check icon `onCorrect’.
Implementation snippet
import React from "react";
import Crossword from "@jaredreisinger/react-crossword";
import { initialData } from "../utils/crossword";
function Play() {
const [data, setData] = React.useState(initialData);
const onCorrect = (...values) => {
try {
const [direction, number, answer] = values;
const newData = { ...data };
newData[direction][number].clue = newData[direction][number].clue + " ✅";
setData(newData);
} catch (error) {
console.log(error);
}
};
return (
<Crossword
data={data}
onCorrect={onCorrect}
/>
);
}
export default Play;
If data props should not be updated, please mention that and I will close the issue.
Thanks.
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
How to Prevent the Error: Cannot Read Property '0' of Undefined
A guide on how to prevent the error "cannot Read Property '0' of Undefined", covering techniques such as try, catch, using const over...
Read more >6.4.2: `TypeError: Cannot read property '0' of undefined ...
js: After executing only the web3.js gas estimation method contract.methods.myMethod().estimateGas() on a function call (that crashes ganache ...
Read more >How to avoid 'cannot read property of undefined' errors?
The error is probably a regular Javascript exception, so try the try..catch statement. That said, an array that contains wildly heterogenous ...
Read more >Avoiding those dang cannot read property of undefined errors
Uncaught TypeError : Cannot read property 'foo' of undefined. The dreaded error we all hit at some point in JavaScript development.
Read more >cannot read properties of undefined (reading 'refresh') - You.com
In your code snippets all the places where any name property is accessed is in an Array.prototype.map callback, which should be safe when...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
The component should handle having the
data
prop updated (any React component should deal with props getting updated, after all!), but I think we can find a better solution for your particular use case, since you’re not really changing the crossword as a whole, you’re just trying to get some additional context-sensitive styling in place.How about if I add a class name to clues that have been answered correctly? They currently have a
clue
class, but it should be easy to addcorrect
once they’ve been answered. Then, you can add CSS (either for your app as a whole, or scoped to the Crossword using something like styled-component) so that you can have:… and not have to do any prop changing (which requires the component to recalculate the entire puzzle).
Thanks @JaredReisinger, this is great. Just what I was looking for.