I have the following problem I can't seem to be able to figure out.
Container.jsx
const [selectedCardTypeList, setSelectedCardTypeList] = useState([]); const onDragEnd = result => { const {destination, source, draggableId} = result; if (!destination) { return; } if (destination.droppableId === source.droppableId && destination.index === source.index ) { return; } let newInputIds = inputs; newInputIds.splice(source.index, 1); newInputIds.splice(destination.index, 0, draggableId); setSelectedCardTypeList(newInputIds); }; return ( <div> <DragDropContext onDragEnd={onDragEnd}> <DroppableWrapper selectedCardTypeList={selectedCardTypeList}> {selectedCardTypeList.map((v, index) => <CardContainer removeCardType={removeCardType} cardType={v} index={index}/> )} </DroppableWrapper> </DragDropContext> </div> ) DropableWrapper.jsx
import {Droppable} from "react-beautiful-dnd"; import React from "react"; export const DroppableWrapper = ({children, selectedCardTypeList}) => { return ( <> <Droppable droppableId="droppable" direction="horizontal"> {(provided, snapshot) => ( <div className={selectedCardTypeList.length ? 'container-horizontal' : 'container-vertical'} ref={provided.innerRef} {...provided.droppableProps} > {children} {provided.placeholder} </div> )} </Droppable> </> )}; export default DroppableWrapper; As you can see I omitted quite a lot of the code, and this is a dummy example but basically what is happening is if I declaratively pass in the children to the parent (using .map), the Child elements will only respect the first state change and despite I can confirm on action the state is being updated, this is not being reflected when rendering {children}.
Any ideas? Thank you!
There is nothing wrong with CardContainer, If I don't abstract the Droppable out as a wrapper just leave it with the container it works.
useStatehook can't "see" if the contents of an array have changed. You should use theuseReducerhook instead.