I'm having a component in React. It's prop group
should update when the state cloneMode
change. For this I'm using the following code:
Structure:
const DraggableElement = ({ list, setList, cloneMode }) => {
return (
<ReactSortable
group={
cloneMode
? { name: "tasks_group", pull: "clone" }
: "tasks_group"
}
key={cloneMode}
list={list}
setList={setList}
animation={200}
delay={1}
className="task-child_drag"
>
{list.map((e) => {
return <TaskItem key={e._id} task={e} />;
})}
</ReactSortable>
);
};
Parent:
const Tasks = () => {
const [cloneMode, setCloneMode] = useState(false);
return (
<div className="tasks">
<DraggableElement
list={todo}
setList={setTodo}
cloneMode={cloneMode}
/>
<DraggableElement
list={inProgress}
setList={setInProgress}
cloneMode={cloneMode}
/>
<DraggableElement
list={done}
setList={setDone}
cloneMode={cloneMode}
/>
</div>
);
};
When I run setCloneMode(true)
, it's not affecting the component. Any thoughts on how can I achieve it?
Without a codepen, any info about why you think the state isn't updating, or any more context it's difficult to answer your question but here is a list of things to try:
data-test={cloneMode}
.key
prop from your DraggableElement
component. You don't need a key here. If you're going to use a key anywhere in React it should be unique. The way you have this setup now all the keys will be either true or false.true
prop value for one of these to make 100% sure that the prop isn't working.First thing is, You're not changing the state (or forgot to write it down). Without it we can't answer your question.
This is the default behavior of React. Component should rerender once the value of props or state is changed
.
So, download React Dev Tools which is a browser extension for Chrome or Edge which helps to see/change the value of props and state from chrome developer window in real time. With this you'll get a better understanding of your Problem, and start to debug from there.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.