简体   繁体   English

防止使用 React 和 React-memo 重新渲染组件

[英]prevent re render component using React and React-memo

I would like to prevent component re-rendering using React.我想防止使用 React 重新渲染组件。 I've read some guides but I'm still having trouble getting my code to work.我已经阅读了一些指南,但我仍然无法让我的代码正常工作。 The CreateItem component creates an input form from the json object. CreateItem 组件从 json 对象创建一个输入表单。 When the input states change, React re-renders all components.当输入状态发生变化时,React 会重新渲染所有组件。 I would avoid this situation as it causes some problems.我会避免这种情况,因为它会导致一些问题。

I have used React.memo but my code still doesn't work.我使用过 React.memo 但我的代码仍然无法正常工作。 Is this a good way to implement this code?这是实现此代码的好方法吗? How can I correct my code?如何更正我的代码? Thank you谢谢


function MyComponent() {
    
    return(
        <div className="row"> 
            {Array.from(new Map(Object.entries(json))).map((data) => (
                <CreateItem obj={data} />

            ))}

        </div>
    );
        
}



//function CreateDiv(props) {
const CreateDiv = React.memo((props) => {

    console.log("rendering ");

    return (
        <form name="myForm"  onSubmit= {formSubmit}>
            <div className="row">
                {Array.from(new Map(Object.entries(props.obj[1]))).map((data) => (
                <>
                {(() => { 
                     return(
                        <div className="col-sm-2">
                            <CreateItem obj={data[1]} />
                        </div>
                    )
                })()}
                </>
                ))}
            </div>
        </form>
    ); 
});

--- EDIT --- - - 编辑 - -

CreateItem uses CreateCheckBoxComponent function to create my custom checkbox with default status from json value. CreateItem 使用 CreateCheckBoxComponent 函数从 json 值创建具有默认状态的自定义复选框。 CreateCheckBoxComponent code is follwing: CreateCheckBoxComponent 代码如下:



function CreateCheckBoxComponent(props) {

    if(parseInt(props.obj.defaultValue) === 5)
        setChecked(false);
    else
        setChecked(true);

    return(
        <FormCheck
                label={props.obj.simbolName} 
                name={props.obj.idVar}
                type="checkbox"
                checked={checked}
                onChange={handleCheckBoxChange}
                sm={10}
            />                 
    );

}

HandleCheckBoxChange works fine and changes state, but when I click on checkbox to change the flag, CreateCheckBoxComponent is re-render and it sets the default state again. HandleCheckBoxChange 工作正常并更改状态,但是当我单击复选框以更改标志时, CreateCheckBoxComponent 重新渲染并再次设置默认状态。 I would like to avoid this problem and I think preventing re-rendering can be a solution..我想避免这个问题,我认为防止重新渲染可以是一个解决方案..

React.memo only prevents own rerendering. React.memo只会阻止自己的重新渲染。

You have considered the following things.你已经考虑了以下几点。

  1. If the children are using React.memo but the parent re-renders the children will render also.如果孩子正在使用React.memo但父母重新渲染,孩子也将渲染。

  2. React.memo prevents re-rendering if the component's state changes.如果组件的状态发生变化, React.memo 会阻止重新渲染。 but if the prop changes, the component re-renders.但如果道具发生变化,组件会重新渲染。

Note: make sure when you render elements/Components with the map function or any iteration always provide a unique key to them.注意:确保在使用 map 函数或任何迭代渲染元素/组件时始终为它们提供唯一键。

For more information click here欲了解更多信息,请点击此处

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

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