My component (Material UI TransferList) receives props.selectedItems as an array of previously selected items. And in props.Items I have all available items.
I expect to see props.selectedItems in the left panel of TransferList. However, the left panel is empty and right panel is full, even though i put there only remainingItems.
function TransferList(props) {
// props.items = [0,1,2,3,4,5,6,7]
console.log(props.selectedItems); // [2, 6]
let remainingItems = not(props.items, props.selectedItems);
console.log(remainingItems); // [0,1,3,4,5,7]
const [checked, setChecked] = useState([]);
const [right, setRight] = useState(remainingItems);
const [left, setLeft] = useState(props.selectedItems);
console.log(right); // [0,1,2,3,4,5,6,7] ?????????????????????????!!!!!!!!!!!!
console.log(remainingItems); // [0,1,3,4,5,7]
console.log(left); // [] ?????????????????????????!!!!!!!!!!!!
...
When i tried useEffect (from this question) it did not help me:
... // the same code above
useEffect(() => {
let remainingItems = not(props.items, props.selectedItems);
setRight(remainingItems);
setLeft(selectedItems);
console.log(right); // [0,1,2,3,4,5,6,7] ?????????????????????????!!!!!!!!!!!!
console.log(remainingItems); // [0,1,3,4,5,7]
console.log(left); // [] ?????????????????????????!!!!!!!!!!!!
}, [props.items, props.selectedItems]);
...
Also, when i use simple array without getting it from props, everything works perfectly!
function TransferList(props) {
items = [0,1,2,3,4,5,6,7];
selectedItems=[2,6];
let remainingItems = not(items, selectedItems);
console.log(remainingItems); // [0,1,3,4,5,7]
const [checked, setChecked] = useState([]);
const [right, setRight] = useState(remainingItems);
const [left, setLeft] = useState(selectedItems);
console.log(right); // [0,1,3,4,5,7]
console.log(remainingItems); // [0,1,3,4,5,7]
console.log(left); // [2,6]
...
What is going on? )))