简体   繁体   English

无需重新渲染父组件即可访问子组件数据

[英]Access child components data without re-rendering parent

I have a parent component which contains a chart, and 2 sliders which define the data that will be used on the chart.我有一个包含图表的父组件和 2 个滑块,它们定义了将在图表上使用的数据。

I want the chart to only re-render with the new values from the sliders when the apply button is clicked.我希望图表仅在单击应用按钮时使用滑块中的新值重新呈现。

The current problem I have is that if I manage the state of the sliders from the parent, then the parent re-renders every time the sliders change value.我目前遇到的问题是,如果我从父级管理滑块的状态,那么每次滑块更改值时父级都会重新渲染。

I am looking for a way that I can have the sliders hold their own state, and only re-render the parent component when the apply button is clicked.我正在寻找一种方法,可以让滑块保持自己的状态,并且仅在单击应用按钮时重新渲染父组件。 Does anyone have any suggestions?有没有人有什么建议?

function Parent() {
  const useState [slider1Value, setSlider1Value] = useState(0);
  const useState [slider2Value, setSlider2Value] = useState(0);
   return (
     <div>
       <Chart
         value1={slider1Value}
         value2={slider2Value}
       />
       <Slider onChange={setSlider1Value}/>
       <Slider onChange={setSlider2Value}/>
       <button>Apply</button>
     <div>
   );     
}

hmm Probably React.memo is your best bet.嗯可能 React.memo 是你最好的选择。 Something along like this像这样的东西

function RenderChart(slider1Value, slider2Value){
return (
     <Chart
         value1={slider1Value}
         value2={slider2Value}
       />
)
}

function Parent() {
  const useState [slider1Value, setSlider1Value] = useState(0);
  const useState [slider2Value, setSlider2Value] = useState(0);
  const useState [apply, setApply] = useState(0);
  const renderedChart = useMemo(() => RenderChart(slider1Value, slider2Value), [apply])
   return (
     <div>
        {renderedChart}
       <Slider onChange={setSlider1Value}/>
       <Slider onChange={setSlider2Value}/>
       <button onClick={()=> setApply(true)}>Apply</button>
     <div>
   );     
}

Haven't had time to test but this is how it looks like to me还没有时间测试,但这就是我的样子

Another probably (hacky) way is to have a placeholder state that only be populated when you click the apply button.另一种可能(hacky)的方法是拥有一个占位符状态,只有在您单击应用按钮时才会填充该状态。

The best way is to put the slider values into refs, so changing them doesn't re-render the parent, then on button click update a state value which contains the current chart values.最好的方法是将滑块值放入 refs,因此更改它们不会重新渲染父级,然后单击按钮更新包含当前图表值的状态值。 Something like this:像这样的东西:

import React,  { useCallback, useState, useRef } from 'react';

const Parent = () => {
    const slider1Value = useRef(0);
    const slider2Value = useRef(0);
    
    const setSlider1Value = useCallback((value) => {
        slider1Value.current = value;
    }, []);
    
    const setSlider2Value = useCallback((value) => {
        slider2Value.current = value;
    }, []);
    
    const [chartValues, setChartValues] = useState({ chart1: 0, chart2: 0 });
    
    const updateChartValues = useCallback(() => {
        setChartValues({
            chart1: slider1Value.current,
            chart2: slider2Value.current
        });
    }, []);

    return (
        <div>
            <Chart
                value1={chartValues.chart1}
                value2={chartValues.chart2}
            />
            <Slider onChange={setSlider1Value}/>
            <Slider onChange={setSlider2Value}/>
            <button onClick={updateChartValues}>Apply</button>
        </div>
     );     
}

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

相关问题 根据父组件的状态来突变子组件,而无需重新渲染 - Mutating child components based on parent component's state, without re-rendering React Redux-父状态发生变化,但子组件未重新呈现 - React redux - parent state changing, but child components not re-rendering 尽管 Parent 中的状态发生了变化,但子组件不会重新渲染 - Child Components not re-rendering despite a state-change in Parent 父组件多次重新渲染子组件ReactJS - Parent component re-rendering child components multiple times ReactJS 停止重新渲染父组件。 仅重新渲染子组件 - Stop Re-Rendering of Parent Components. Only Re-Render Child Component React 在重新渲染父组件时如何重用子组件/保留子组件的 state? - How does React re-use child components / keep the state of child components when re-rendering the parent component? 如何在不重新渲染父级的情况下从另一个子级更改子级状态 - How to change child state from another child without re-rendering parent 如何使用 React 功能组件在不重新渲染 Parent 的情况下更新 Props - How To Update Props without re-rendering Parent using React Functional Components Reactjs:为什么子组件不会在数据更改时重新呈现? - Reactjs: Why child components aren't re-rendering on data change? 父组件在父状态更改时不必要地重新渲染子级 - Parent component unnecessarily re-rendering child on parent state change
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM