簡體   English   中英

如何將文本附加到在 React JS 中創建的文本區域?

[英]How to append text to text area created in React JS?

我有以下代碼可以創建一個文本區域。

interface IReceiverProps {
    receivedMessage: string;
    topic: string;
}

export default class Receiver extends React.Component<IReceiverProps, {}> {

    render() {
        var textAreaStyle = {
            width: 1300,
            height: 450,
            border: '3px solid #cccccc',
            padding: '5px',
            fontFamily: 'Tahoma, sans-serif',
            overflow: 'auto',
            marginLeft: '10px'
        }
        return (
            <textarea style={textAreaStyle} value={this.props.receivedMessage}/>
        );
    }

}

此接收到的消息由另一個組件傳遞。 如何在此文本區域中將收到的消息附加到另一個下面? 任何幫助將非常感激。

使用名為 textMessage 的狀態。

constructor(props) {
  super(props);
  this.state = {
    textMessage: props.receivedMessage
  };
}

在 componentWillReceiveProps 中,附加到 textMessage。

componentWillReceiveProps(nextProps) {
  if (nextProps.receivedMessage !== this.props.receivedMessage) {
    this.setState({
      textMessage: `${this.state.textMessage}\n{nextProps.receivedMessage}`
    });
  }
}

綁定到 textMessage。

<textarea style={textAreaStyle} value={this.state.textMessage} />

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM