假设我有这个 useSelector:
const mapState = ({ data }) => ({
Items: data.items,
});
items 是一个数组。这是在 JSON 对象中:
[
{
itemName: "Item1",
itemPrice: "5",
},
{
itemName: "Item2",
itemPrice: "5",
},
];
这些是代码:
const Item = () => {
const { items } = useSelector(mapState);
const dispatch = useDispatch();
useEffect(() => {
dispatch(getData(Items));
}, []);
//store a data item.name inside the useState
const [itemName, setItemName] = useState("");
return (
<Container>
</Container>
);
};
export default Item;
是否可以从 useSelector 的数据中设置 itemName 的初始值?喜欢这样的吗?
const [itemName, setItemName] = useState(itemName);