有没有办法使用反应附加组件?

is there a way to append a component using react?

基本上我希望用户单击一个按钮,该按钮将向页面添加一个 <Message /> 组件。他们可以继续点击按钮,然后将创建更多 <Message /> 个组件。

伪代码

render() {
    return (
        <button onClick={this.addMessage}></button>
        <Message />
        //basically a new message component would spawn here so it would look like this if the person clicked the button 3 times

        <Message /> 
        <Message /> 
        <Message /> 

    );

}

addMessage = () => {
   create new Message component
}

使用状态对象来做你想做的事。例如,

const [messages, setMessages] = useState([]);
const wasClick = (data) => {
    setMessages([...messages, data]);
}

然后在渲染函数中

messages.map(message=><Message message={message}/>)