【发布时间】:2020-07-07 07:06:45
【问题描述】:
我有一个名为“Causes”的父组件和一个名为“Graph”的子组件,
有一个名为“datas”的钩子,它在“Causes”(父级)中创建和更新,我将它作为道具传递给“Graph”(子级)。
第一次,一切正常,但是当我更新“原因”(父)中的“数据”时,“图形”(子)仍然有旧的“数据”对象数组。
如何强制重新渲染子组件?
const [datas, setDatas] = useState([
{ shop: "00h-8h", value: 250, color: "#A2AAC2" },
{ shop: "8h-12h", value: 420, color: "#A2AAC2" },
{ shop: "12h-16h", value: 500, color: "#A2AAC2" },
{ shop: "16h-20h", value: 80, color: "#A2AAC2" },
{ shop: "20h-00h", value: 80, color: "#A2AAC2" }
]);
useEffect(() => {
setDatas(newArray); <- this updates data, but the component below always got the old datas
}, []);
return (
<Graph
h={400}
w={900}
data={datas}
defaultKeys={["shop", "value"]}
/>
)
【问题讨论】:
-
我认为这可能会对您有所帮助:stackoverflow.com/questions/54843675/…
-
需要看看你是如何创建
newArray它需要一个新的参考
标签: javascript reactjs