小编Tho*_*jka的帖子

对于呈现两次的同一组件,React 构造函数仅调用一次

我希望这个切换能够工作,但不知何故,组件的构造函数<A/>仅被调用一次。https://codesandbox.io/s/jvr720mz75

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

class App extends Component {
  state = { toggle: false };
  render() {
    const { toggle } = this.state;
    return (
      <div>
        {toggle ? <A prop={"A"} /> : <A prop={"B"} />}
        <button onClick={() => this.setState({ toggle: !toggle })}>
          toggle
        </button>
      </div>
    );
  }
}

class A extends Component {
  constructor(props) {
    super(props);
    console.log("INIT");
    this.state = { content: props.prop };
  }

  render() {
    const { content …
Run Code Online (Sandbox Code Playgroud)

reactjs

5
推荐指数
1
解决办法
1683
查看次数

标签 统计

reactjs ×1