gpt4 book ai didi

reactjs - 将元素添加到状态 React

转载 作者:行者123 更新时间:2023-12-03 22:53:47 26 4
gpt4 key购买 nike

我已经有了这样的状态:

 this.setState({
conversation:
(
<div>
{conversation.map(element => {
if (element.id === this.props.id) {
return(
<div className="row msg_container base_sent">
<div className="col-md-10 col-xs-10">
<div className="messages msg_sent">
<p>{element.message}</p>
</div>
</div>
</div>
)
}
else {
return(
<div className="row msg_container base_receive">
<div className="col-md-10 col-xs-10">
<div className="messages msg_receive">
<p>{element.message}</p>
</div>
</div>
</div>
)
}
})}
</div>
)
})

现在我想用新信息更新它。所以添加另一个 div 到它。

类似的东西:
this.setState({conversation: previousConversation + new div})

我该怎么做?或者我需要从零设置一个新状态

最佳答案

根据 React 文档( webarchive source ):

What Should Go in State?

State should contain data that a component's event handlers may change to trigger a UI update. In real apps this data tends to be very small and JSON-serializable. When building a stateful component, think about the minimal possible representation of its state, and only store those properties in this.state. Inside of render() simply compute any other information you need based on this state. You'll find that thinking about and writing applications in this way tends to lead to the most correct application, since adding redundant or computed values to state means that you need to explicitly keep them in sync rather than rely on React computing them for you.

What Shouldn’t Go in State?

this.state should only contain the minimal amount of data needed to represent your UI's state. As such, it should not contain:

  • Computed data: Don't worry about precomputing values based on state — it's easier to ensure that your UI is consistent if you do all computation within render(). For example, if you have an array of list items in state and you want to render the count as a string, simply render this.state.listItems.length + ' list items' in your render() method rather than storing it on state.
  • React components: Build them in render() based on underlying props and state.
  • Duplicated data from props: Try to use props as the source of truth where possible. One valid use to store props in state is to be able to know its previous values, because props can change over time.


在您的情况下,将您的 html 移动到您的渲染功能中。然后在您的状态中存储一个条件或数据,用于触发将另一个 div 添加到您的 html 中,方法是通过有条件地呈现一些 html 或基于向您的状态添加更多数据将另一个 div 添加到 .map 函数内的数组.

例子:
Class Example render React.Component{
state = {
comments: [
{ message:"comment 1", id: 1, timeStamp: "" },
{ message:"comment 2", id: 2, timeStamp: "" },
{ message:"comment 3", id: 3, timeStamp: "" }
]
}

componentDidMount = () => {
//update data from api...
.then(data => this.setState({ comments: data }))
}

render(){
conversation.map(element => {
if (element.id === this.props.id) {
return(
<div className="row msg_container base_sent">
<div className="col-md-10 col-xs-10">
<div className="messages msg_sent">
<p>{element.message}</p>
</div>
</div>
</div>
)
}
else {
return(
<div className="row msg_container base_receive">
<div className="col-md-10 col-xs-10">
<div className="messages msg_receive">
<p>{element.message}</p>
</div>
</div>
</div>
)
}
})
}
}

关于reactjs - 将元素添加到状态 React,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47875097/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com