简体   繁体   English

React Components 子组件渲染两次 - 有什么方法可以解决这个问题?

[英]React Components child component render twice - any way to fix this?

When i click on Update Key button then Child1 component render every time so how can i resolve it.  
Note : I have create three component one parent component and two child component when i click on 
       parent component button to update child component, so child1 component increase to display 
       multiple time. so please resolved this as soon as possible.

When i click on Update Key button then Child1 component render every time so how can i resolve it.当我单击更新密钥按钮时,Child1 组件每次都会渲染,所以我该如何解决它。
Note: I have create three component one parent component and two child component when i click on parent component button to update child component, so child1 component increase to display multiple time.注意:当我单击父组件按钮更新子组件时,我创建了三个组件一个父组件和两个子组件,因此 child1 组件增加以显示多次。 so please resolved this as soon as possible.所以请尽快解决这个问题。

import React, { useState } from "react";
import ReactDOM from "react-dom";

class Child1 extends React.Component {
  componentDidMount() {
    console.log("mounted");
  }
  render() {
    console.log("rendered");
    return <div>Child</div>;
  }
}

class Child2 extends React.Component {
  componentDidMount() {
    console.log("mounted2");
  }
  render() {
    console.log("rendered2");
    return <div>Child2</div>;
  }
}

class App extends React.Component {
  state = {
    counter: 0,
    counter2: 0
  };

onCounter = () => this.setState({ counter: this.state.counter + 1 , counter2: this.state.counter2 + 1 });

  render() {
    return (
      <>
        <Child1 key={this.state.counter} />
        <Child2 key={this.state.counter2} />
        <button onClick={this.onCounter}>Update Key</button>
      </>
    );
  }
}

ReactDOM.render(<App />, document.getElementById("root"));

my first response, I think I understand the question, I rewrote it using react hooks as it makes it easier to read and requires no configuration changes just make sure your running the latest version of react.我的第一个回答,我想我理解了这个问题,我使用 react 钩子重写了它,因为它更容易阅读并且不需要更改配置,只需确保您运行最新版本的 react。

import React, { useState } from "react";
import ReactDOM from "react-dom";

function Child1(props) {
  return <h1>Child{props.input}</h1>;
}
function Child2(props) {
  return <h1>Child{props.input}</h1>;
}

function App() {
  let [counter, setCounter] = useState(0);
  let [counter2, setCounter2] = useState(1);

  let onCounter = () => {
    setCounter(counter+1)
    setCounter2(counter2+1)
  }

  return (
    <>
      <Child1 input={counter} />
      <Child2 input={counter2} />
      <button onClick={onCounter}>Update Key</button>
    </>
  );
}


ReactDOM.render(<App />, document.getElementById("root"));

It is because your Child component, ie, Child1 and Child2 are using the same value as key .这是因为您的 Child 组件,即Child1Child2使用与key相同的值。 Both counter and counter2 states have the same value and it is creating ambiguity in the ReactDOM. countercounter2状态都具有相同的值,这会在 ReactDOM 中产生歧义。 Here, you are not iterating over an array so there's is no need of using the key property.在这里,您没有遍历array ,因此不需要使用key属性。 It will work fine.它会正常工作。 If you've to use the key property, make sure they are unique at the same level.如果您必须使用key属性,请确保它们在同一级别上是唯一的。

~Prayag ~Prayag

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

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