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.

TypeError: Cannot set property of null

See original GitHub issue

Hey, I’m trying out 3.4. I got pretty far but just hit this nasty bug and can’t figure it out after a few hours of fighting with it so I’m just giving up.

// app.ts
const [state, actions] = useLocalStore<IFoo>({
  selectElement: action((_state, id) => {
    const oldSelectedNode = getSelectedNode(_state.nodes);
    oldSelectedNode.selected = false;
    const newSelectedNode = getNodeById(_state.nodes, id); // this returns null even though unit tests with mocked data work
    newSelectedNode.selected = true; // fails because null.selected
  }),
});
// ...
return <RecursiveElement nodes={state.nodes} actions={actions} />;

// RecursiveElement.ts
import { Element } from 'my-styled-components';

// ...
<Element
  // @ts-ignore
  as={node.tag}
  key={node.id}
  node={node} // pass this on to the styled-component
  onClick={(e) => {
    e.stopPropagation();
    console.log(node.id); // 🕵️‍♂️ works
    actions.selectElement(node.id); // 💩🛏
  }}
>
  {!!node.nodes.length && (
    <RecursiveElement nodes={node.nodes} actions={actions} />
  )}
</Element>;

Use case:

  • I have a recursive component (rendering out a tree).
  • When I click a component, I want to set ALL nodes to have “selected = false” and the clicked on component to get set to “selected: true”

What I’m trying:

  • set store using useLocalStore within <Wrapper>
  • pass state and actions to a child [of <Wrapper>] component
  • in the child component, hit a utility function that steps through the tree figuring out what the selected node is, and also the node to get by ID.

Seems like a race condition. I’d try to debug it longer if the entire thing wasn’t mangled in whatever that proxy stuff is… 😩

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
ctrlplusbcommented, May 28, 2020

#0618

😊

0reactions
corysimmonscommented, May 28, 2020

image

In Discord, need the #1234 as well

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cannot set properties of Null in JavaScript | bobbyhadz
The "Cannot set property of null" error occurs when trying to set a property on a null value. Variables that store a value...
Read more >
Uncaught TypeError: Cannot set property 'value' of null
The problem is that you haven't got any element with the id u so that you are calling something that doesn't exist. To...
Read more >
Uncaught TypeError: cannot set property 'innerHTML' of null
Today you learned why you sometimes see the 'cannot set property of null' error in HTML/JavaScript. To recap, this error occurs commonly when...
Read more >
Uncaught TypeError: Cannot set property
The null is a special assignment value, which can be assigned to a variable as a representation of no value . If you...
Read more >
Uncaught TypeError: Cannot set property 'data' of null in ...
cshtml file and give me an error as follow. Uncaught TypeError: Cannot set property 'data' of null at ra (jquery.dataTables.min.js:35) at Ub ...
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