【发布时间】:2022-11-19 06:54:34
【问题描述】:
下面的反应代码呈现一个图表,当按下按钮时,该图表会使用其他数据进行更新。计数状态作为道具传递给 BarChart 函数。但是,图表不会在数据更改时自动更新。如何实现?
import React, { useState, useEffect } from 'react';
import Plot from 'react-plotly.js';
function main() {
const [count, setCount] = useState([1,2,3]);
return(
<>
<BarChart value={count}/>
<button onClick={() => setCount([...count, 123])}/>
</>
)
}
const BarChart = (count) => {
return (
<div>
<Plot
data={[
{type: 'scatter',
x: ['one','two','three'],
y: count,
marker: {color: 'red'}
}]}
layout={{width: 1000, height: 500, title: "hello"}}
/>
</div>
)
}
【问题讨论】: