question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

On updating data prop, app crashes with error "Cannot read property '0' of undefined"

See original GitHub issue

When 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:closed
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
JaredReisingercommented, May 15, 2020

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 add correct 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:

.clue.correct::before {
  content: "\2705 ";  /* show check mark before correctly answered clues */
}

… and not have to do any prop changing (which requires the component to recalculate the entire puzzle).

0reactions
dreamer01commented, May 27, 2020

Thanks @JaredReisinger, this is great. Just what I was looking for.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found