简体   繁体   English

停止重新渲染父组件。 仅重新渲染子组件

[英]Stop Re-Rendering of Parent Components. Only Re-Render Child Component

I have a React-Select Field, inside a Formik Field, that when you select an item from the dropdown options, all the Parent Components are rerendered.我在 Formik 字段中有一个 React-Select 字段,当您从下拉选项中选择一个项目时,所有父组件都会重新呈现。 This is the deepest child component available in the Container.这是容器中可用的最深的子组件。

And it re-renders 4 Parents.它重新渲染了 4 个父级。 Which is kind of Problematic.这有点问题。 I want to limit the rerender of the Component to only itself.我想将组件的重新渲染限制为仅其自身。

The above happens because each Child Process passes
props to the Container, which is the master form.
And onSubmit it takes all the info(props) and makes the API Call.

I tried doing it with shouldComponentUpdate but no luck.我试过用shouldComponentUpdate但没有运气。 I tried to do it with SetState , but that though fell in the water, as I couldn't make it work(Got a ton of errors).我试图用SetState来做到这一点,但这虽然掉入水中,因为我无法让它工作(有很多错误)。

--TLDR-- THE PROBLEM: Make a Component retain the rendering to only itself. --TLDR--问题:使组件仅保留其自身的渲染。 External Components used in it Formik and React-Select .其中使用的外部组件FormikReact-Select

Here is the code for that:这是代码:

            <div className="w-50">
              <Field
                name={`${keyField}.${index}.permissions`}
                render={({ field: { value, name }, form: { setFieldValue, setFieldTouched } }) => (
                  <div>
                    <label htmlFor="namespace-permissions" className="font-weight-medium">
                      Permissions in Namespace <Asterisk />
                    </label>
                    <Select
                      isMulti
                      closeMenuOnSelect={false}
                      id="namespace-permissions"
                      defaultValue={convertNamespaceToDefaultValue(
                        dependencies.namespacePermissions,
                        value
                      )}
                      options={convertNamespaceToSelect(dependencies.namespacePermissions)}
                      onChangeCallback={values => {
                        setFieldValue(name, convertSelectToNamespacesData(values));
                        setFieldTouched(name, true);
                      }}
                    />
                    <ErrorMessage name={name} component={FormErrorMessage} />
                  </div>
                )}
              />
            </div>

The dependacies prop is what makes the trip up the tree, to the master form Props, and rerenders the entire Component Tree. dependacies prop 是使树上的旅程,到主窗体 Props,并重新渲染整个组件树的原因。 This also, ties with another question I had yesterday, about react-select's closeMenuOnSelect={false} not working correctly.这也与我昨天遇到的另一个问题有关,关于 react-select 的closeMenuOnSelect={false}无法正常工作。

^This is the reason why that happens. ^这就是发生这种情况的原因。 Thank you..谢谢..

I don't know how you would be able to do this with the libraries that you're using.我不知道您如何使用您正在使用的库来做到这一点。 But when I don't want my components rendering unnecessarily I use React.memo it will shallow compare the props object and decide if needs to update.但是当我不希望我的组件不必要地渲染时,我使用React.memo它会浅比较props对象并决定是否需要更新。

From React DOCS 来自 React DOCS

WITHOUT REACT MEMO无反应备忘录

 function App() { return( <Parent1/> ); } function Parent1(props) { console.log('Rendering Parent1...'); const [parentState,setParentState] = React.useState(true); return( <Parent2 setParentState={setParentState} /> ); } function Parent2(props) { console.log('Rendering Parent2...'); return( <Child setParentState={props.setParentState} /> ); } function Child(props) { console.log('Rendering Child...'); return( <button onClick={()=>props.setParentState((prevState)=>!prevState)}>Update ParentState</button> ); } ReactDOM.render(<App/>, document.getElementById('root'));
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.3/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.3/umd/react-dom.production.min.js"></script> <div id="root"/>

WITH REACT.MEMO带有反应备忘录

 function App() { return( <Parent1/> ); } function Parent1(props) { console.log('Rendering Parent1...'); const [parentState,setParentState] = React.useState(true); return( <Parent2 setParentState={setParentState} /> ); } const Parent2 = React.memo(function Parent2(props) { console.log('Rendering Parent2...'); return( <Child setParentState={props.setParentState} /> ); } ); const Child = React.memo(function Child(props) { console.log('Rendering Child...'); return( <button onClick={()=>props.setParentState((prevState)=>!prevState)}>Update ParentState</button> ); } ); ReactDOM.render(<App/>, document.getElementById('root'));
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.3/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.3/umd/react-dom.production.min.js"></script> <div id="root"/>

I would check if Formik's onSubmit is being called and if that's triggering the tree to re-render.我会检查 Formik 的 onSubmit 是否被调用,以及是否触发了树重新渲染。 If you have a button with type=button that could be triggering a form submit.如果您有一个type=button可能会触发表单提交。

There is also a bug with Formik before v2 where Field will mount and unmount all of it's children on every update if given the render function through render or component prop.在 v2 之前的 Formik 也有一个错误,如果通过rendercomponent道具给定渲染功能,则 Field 将在每次更新时挂载和卸载它的所有子项。 Instead just pass the render function as the Fields child.而是将渲染函数作为 Fields 子项传递。

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

相关问题 父组件多次重新渲染子组件ReactJS - Parent component re-rendering child components multiple times ReactJS 父组件状态改变,子组件不重新渲染 - State changed in parent component, child components do not re-render React 在重新渲染父组件时如何重用子组件/保留子组件的 state? - How does React re-use child components / keep the state of child components when re-rendering the parent component? 如何在React Native中阻止子组件重新呈现父组件? - How to stop a child component from re-rendering a parent component in react native? 父组件在父状态更改时不必要地重新渲染子级 - Parent component unnecessarily re-rendering child on parent state change 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 无需重新渲染父组件即可访问子组件数据 - Access child components data without re-rendering parent 根据父组件的状态来突变子组件,而无需重新渲染 - Mutating child components based on parent component's state, without re-rendering 如何在父组件的道具发生变化时停止重新渲染子组件? - How to stop re-rendering of child component when its parent's props change?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM