简体   繁体   English

状态更改后更新React子组件

[英]Updating React child component after state change

I made a text input box which, when submitted, will update the state of the messages in the parent component. 我做了一个文本输入框,提交后将更新父组件中消息的状态。 The messages in the parent component are passed down to the message display. 父组件中的消息将向下传递到消息显示。 I'd like the component responsible for displaying the messages to update and display the messages after each submission, but can't figure out how to do it. 我希望负责显示消息的组件在每次提交后更新并显示消息,但无法弄清楚该怎么做。 I made a code sandbox here: 我在这里制作了一个代码沙箱:

https://codesandbox.io/s/unruffled-pasteur-nz32o https://codesandbox.io/s/unruffled-pasteur-nz32o

Here's my code: 这是我的代码:

Parent component: 父组件:

import React, { Component } from "react";
import Messages from "./Messages";
import Input from "./Input";

export default class Container extends Component {
  constructor(props) {
    super(props);
    this.state = {
      messages: []
    };
  }

  updateMessage(message) {
    this.state.messages.push(message);
  }

  render() {
    return (
      <div>
        <Messages messages={this.state.messages} />
        <Input updateMessage={message => this.updateMessage(message)} />
      </div>
    );
  }
}

Message input component: 消息输入组件:

import React, { Component } from "react";

export default class Input extends Component {
  constructor(props) {
    super(props);
    this.state = {
      message: ""
    };
 }

  sendMessage() {
    this.props.updateMessage(this.state.message);
    this.setState({ message: "" });
  }

  render() {
    return (
      <div>
        <input
          type="text"
          value={this.state.message}
          onChange={({ target }) => {
            this.setState({ message: target.value });
          }}
        />
        <button onClick={() => this.sendMessage()}>Send</button>
      </div>
    );
  }
}

Message display component: 消息显示组件:

import React, { Component } from "react";

export default class Messages extends Component {
  render() {
    return this.props.messages.map(message => {
      return <div>{message}</div>;
    });
  }
}

Thanks! 谢谢!

In your updateMessage(message) method, can you try: 在您的updateMessage(message)方法中,您可以尝试:

updateMessage(message) {
    let { messages } = this.state;
    messages.push(message)
    this.setState({ messages })
}

From your code: 从您的代码:

updateMessage(message) {
  this.state.messages.push(message);
}

You're modifying the state directly and you're not supposed to do that (except for in the constructor). 您正在直接修改状态, 而不应该这样做 (构造函数中除外)。 It won't cause a re-render in this way. 这样不会导致重新渲染。 Instead, clone the state, modify it, then update the state via setState . 而是克隆状态,进行修改,然后通过setState更新状态。 Calling setState will invoke a re-render. 调用setState将调用重新渲染。

updateMessage(message) {
  this.setState({
    messages: [...this.state.messages, message],
  });
}

Your error is in this part 您的错误在于这部分

updateMessage(message) {
    this.state.messages.push(message);
  }

You can not change state directly. 您不能直接更改状态。 You must use setState() method to change state 您必须使用setState()方法更改状态

updateMessage(message) {
        this.setState({
             messages : [...this.state.messages, message]
        });
      }

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

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