简体   繁体   English

react-chartjs-2响应式热刷新(饼图)

[英]react-chartjs-2 responsive hot refresh (pie chart)

I'm currently working on a pie chart for which I want to change the layout depending on the size of the window (namely change the position of the legend from left to bottom). 我目前正在制作一个饼图,我想根据窗口的大小更改布局(即,将图例的位置从左到下更改)。

function PieChart() {
    const { width } = useWindowSize();

    return (
        <React.Fragment>
            <Pie
                data={data}
                width={width < size.smallDevices ? 100 : "auto"}
                legend={width < size.smallDevices ? legendOptsSmall : legendOptsLarge}
            />
            <div>{width < size.smallDevices ? "Small devices" : "Big devices"}</div>
        </React.Fragment>
    );
}

In this example, where width returns the width of the window, the div correctly changes from "Small devices" to "Big devices" while I resize the window, but the Pie from react-chartjs-2 doesn't change. 在此示例中,width返回窗口的宽度,在我调整窗口大小时,div正确地从“小型设备”更改为“大型设备”,但是react-chartjs-2的Pie不变。 However, when refreshed, the Pie correctly loads with the right layout (for small devices or big devices, depending on the situation). 但是,刷新后,Pie会以正确的布局正确加载(对于小型设备还是大型设备,取决于情况)。 Only the hot refresh is failing. 仅热刷新失败。

Any idea on how to get a hot refresh? 对如何进行热刷新有任何想法吗?

Thanks! 谢谢!

I agree with Cozzani You should set width/height in your state then add width/height on addEventListener() for resizing and mean while you should update your state then component will re-render, when component re-render your width/height will be updated. 我同意Cozzani意见。您应该在状态中设置width / height,然后在addEventListener()上添加width / height以进行大小调整,这意味着在更新状态时组件将重新渲染,当组件重新渲染时,width / height为更新。 You can have a look on example gist 你可以看看示例要点

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM