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.

useState() not rendering from redux, elements disappear

See original GitHub issue

Hi guys, for some weird reason, state is not updating based on updates from redux. I’ve implemented the following.

FC<...Props> = ({ dispatch, data }) => {
  const [dataState, setDataState] = useState(data);

  useEffect(() => {
    setDataState(data);
  }, [dispatch, data]);
  
  return(
    <ReactFlowProvider>
       <ReactFlow elements={dataState}
     ...
  )
}

And, in my Reducer. I would push a new element using:

case 'CREATE_ITEM':
      return update(
        state,
        {
          map: {
            counter: { $set: state.map.counter + 1 },
            data: {
              $push: [{
                id: state.map.counter + 1,
                className: `context-${state.map.counter + 1}`,
                animated: true,
                type: 'default',
                ...
              }],
            }
          },
        },
      );

In essence, after I update the state again. All elements disappear from the canvas.


Curious if you guys had any feedback, thank you!

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
joriskalzcommented, Dec 11, 2020

I had the same issue, my problem was not to append the item to the existing array. Try the following for adding new items:

useEffect(() => {
      setDataState(e => e.concat( data.map(m => ({ id: m.id, position: m.position, data: { label: m.data.label }, style: { border: m.style.boder, background: m.style.background } }))
) );
}, [dispatch, data]);
0reactions
moklickcommented, Dec 23, 2020

closed due to inactivity

Read more comments on GitHub >

github_iconTop Results From Across the Web

Need help accessing state from useSelector, state disappears ...
1 Answer 1 ... I think you used the wrong property name. Where is state. todoList ? ... when I have state.todos it...
Read more >
React.useEffect Hook – Common Problems and How to Fix ...
The infinite re-renders problem. The reason our component is re-rendering is because our useEffect dependency is constantly changing. But why?
Read more >
Using the State Hook - React
A Hook is a special function that lets you “hook into” React features. For example, useState is a Hook that lets you add...
Read more >
Understanding common frustrations with React Hooks
React Hooks can be frustrating despite their popularity and widespread use. Learn about some of the drawbacks to using React Hooks.
Read more >
React.js — Basic Hooks (useState, useEffect, & useContext)
It's not that writing class-based components in JavaScript are a bad thing, ... useState(), as defined above, hooks state into a component.
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