TypeError: Cannot set property of null
See original GitHub issueHey, 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
useLocalStorewithin <Wrapper> - pass
stateandactionsto 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:
- Created 3 years ago
- Comments:11 (5 by maintainers)
Top 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 >
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

#0618😊
In Discord, need the #1234 as well