基本上,我希望用户单击将向页面添加组件的按钮。他们可以继续单击该按钮,然后将创建更多组件。
伪代码
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}/>)