简体   繁体   English

如何在 Highcharts 事件侦听器中访问 React 钩子状态

[英]How to access React hook state in Highcharts event listener

I'm trying to update state in an event listener using React hooks by adding to the existing state:我正在尝试通过添加到现有状态来使用 React 钩子更新事件侦听器中的状态:

import React, { useState } from 'react';
import { render } from 'react-dom';
import HighchartsReact from 'highcharts-react-official';
import Highcharts from 'highcharts';

const LineChart = () => {
  const [hoverData, setHoverData] = useState(null);
  const [hoveredOverCategories, setHoveredOverCategories] = useState([]);
  const [chartOptions, setChartOptions] = useState({
    xAxis: {
      categories: ['A', 'B', 'C'],
    },
    series: [
      { data: [1, 2, 3] }
    ],
    plotOptions: {
      series: {
        point: {
          events: {
            mouseOver(e){
              console.log(hoveredOverCategories); // Always prints the empty array `[]`
              setHoverData(e.target.category);
              setHoveredOverCategories(
                [...hoveredOverCategories, e.target.category]
              );
            }
          }
        }
      }
    }
  });

  return (
      <div>
        <HighchartsReact
          highcharts={Highcharts}
          options={chartOptions}
        />
        <h3>Hovering over {hoverData}</h3>
        <ol>
          {hoveredOverCategories.map(category => (
            <li>Hovered over {category}</li>
          ))}
        </ol>
      </div>
    )
}

render(<LineChart />, document.getElementById('root'));

However, it seems like the event listener ( mouseOver ) doesn't have access to updated state ( hoveredOverCategories ): console.log(hoveredOverCategories);但是,事件侦听器( mouseOver )似乎无权访问更新状态( hoveredOverCategories ): console.log(hoveredOverCategories); always prints the empty array [] .总是打印空数组[]

The expected result is that the array of categories is added to for each mouseOver event.预期的结果是为每个mouseOver事件添加类别数组。 See this demonstration .请参阅此演示 But what actually happens is that the array is overwritten with only the most recent value.但实际发生的情况是数组仅被最近的值覆盖。

(This reproduction is based on the documented optimal way to update , demonstrated here .) (此复制基于记录在案的最佳更新方式在此处演示。)

Note that I was able to get the expected results using a class component.请注意,我能够使用类组件获得预期的结果。

I think that accessing the hoveredCategories via closure isn't the more precise one.我认为通过闭包访问hoveredCategories并不是更精确的方法。 I was able to make it work by accessing it using the previous state instead of relying on the closure value:我能够通过使用先前的状态而不是依赖闭包值来访问它来使其工作:

mouseOver(e){
  console.log(hoveredOverCategories);
  setHoverData(e.target.category);
  setHoveredOverCategories((prevHoveredOverCategories) => {
    return [...prevHoveredOverCategories, e.target.category]
  });
}

Have a look at it working: https://stackblitz.com/edit/highcharts-react-hook-state-event-listener-xxqscw看看它的工作原理: https : //stackblitz.com/edit/highcharts-react-hook-state-event-listener-xxqscw

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

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