【问题标题】:Correctly push to an array that has been passed as props正确推送到作为道具传递的数组
【发布时间】:2019-07-08 11:49:14
【问题描述】:

我定义了一个数组。

然后将该数组作为道具传递给控制台子节点。

main.js

class Main extends Component {
  constructor() {
    super();
    this.moduleArray = [];
    this.state = {
      moduleArray: this.moduleArray
    };
  }
  render() {
    return (
      <div id="builder-root">
        <Console moduleArray={this.moduleArray} />
      </div>
    );
  }
}
}
export default Main;


我将数组分配给const

然后 onClick 我将一个参数传递给运行 switch 语句的append 函数。然后我将匹配案例的内容推送到数组中。

console.js

class Console extends React.Component {
  render() {
    const { moduleArray } = this.props;
    const append = x => e => {
      switch (x) {
        case 0:
          console.log("case0");
          moduleArray.push(
            <div
              key={moduleArray.length}
              id={moduleArray.length}
              style={{ fontSize: 0, lineHeight: 0 }}
            >
              <Mod1 />
            </div>
          );
          console.log("pushed");
          break;
        //other switch cases
        default:
      }
      this.setState({
        moduleArray: this.moduleArray
      });
    };
    return (
      <div id="console">
        <input onClick={append(0)} value="Single col" type="submit" />
        //other clicks passing parameters
      </div>
    );
  }
}
export default Console;

然后.....什么也没有发生。好吧,我什么也不说。该函数运行并打印控制台日志,我没有收到错误。但内容不呈现。

我认为我需要以某种方式使用spread 运算符,但我不确定并且很难在这种情况下找到任何阅读材料。

【问题讨论】:

  • 在您的逻辑案例中,您应该接收道具并渲染它们并更改 仅父组件的状态因为不变性原则,就像 Felix 的回答所说
  • 您是如何在main.js 组件中使用moduleArray 的?

标签: javascript reactjs ecmascript-6 jsx


【解决方案1】:

Console 组件仅在其道具或状态发生变化时才会呈现。道具只能由组件的父级更改。 State 是一个组件内部对象,由组件本身更改(但可能取决于 props 或其他计算)。

并且道具是不可变的。这意味着你不能用

覆盖它们
moduleArray.push(
            <div
              key={moduleArray.length}
              id={moduleArray.length}
              style={{ fontSize: 0, lineHeight: 0 }}
            >
              <Mod1 />
            </div>
          );

您甚至将moduleArray 声明为const。更改组件父级中的moduleArray(通过回调函数)或使用组件的道具初始化状​​态并使用this.setState(/*...*/)更改状态。

下面列出了一种可能的解决方案:

class Main extends Component {
    constructor(props) {
        super(props);

        this.state = {
            moduleArray: [] // initialize empty or use props to init state
        };
    }

    componentDidMount() {
        // example
        // this.setState({moduleArray: serverResponse.modules}
    }

    render() {
        return (
            <div id="builder-root">
                <Console moduleArray={this.state.moduleArray} addModule={(module) => this.setState({
                    moduleArray: [
                        ...this.state.moduleArray,
                        module
                    ]
                })}/>
            </div>
        );
    }
}
}
export default Main;
class Console extends React.Component {
    render() {
        const {
            moduleArray,
            addModule
        } = this.props;


        const append = x => e => {
            switch (x) {
                case 0:

                    addModule(
                        <div
                            key={moduleArray.length}
                            id={moduleArray.length}
                            style={{fontSize: 0, lineHeight: 0}}
                        >
                            <Mod1/>
                        </div>
                    );

                    break;
                //other switch cases
                default:
            }
        };
        return (
            <div id="console">
                <input onClick={append(0)} value="Single col" type="submit"/>
                {/*other clicks passing parameters*/}
            </div>
        );
    }
}

export default Console;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-03-22
    • 2020-07-12
    • 1970-01-01
    • 2021-12-09
    • 2021-12-07
    • 1970-01-01
    • 2021-05-14
    相关资源
    最近更新 更多