Reactjs 天才聊天中的id是什么?
在这里,我试图理解此链接中可用的代码。但在这里,我无法理解他们为什么使用2个id(消息:[{u id:1, //代码 用户:{ _id:2, //代码 }]) 在setState函数中,他们正在render()方法中写入1个id(_id:1)。另外,在setState函数中传递的id 1和2与render()方法中给出的id之间有什么区别 下面是下面的代码片段: 从“React”导入React 从“react native gifted chat”导入{GiftedChat}Reactjs 天才聊天中的id是什么?,reactjs,react-native,react-native-gifted-chat,Reactjs,React Native,React Native Gifted Chat,在这里,我试图理解此链接中可用的代码。但在这里,我无法理解他们为什么使用2个id(消息:[{u id:1, //代码 用户:{ _id:2, //代码 }]) 在setState函数中,他们正在render()方法中写入1个id(_id:1)。另外,在setState函数中传递的id 1和2与render()方法中给出的id之间有什么区别 下面是下面的代码片段: 从“React”导入React 从“react native gifted chat”导入{GiftedChat} class Exa
class Example extends React.Component {
state = {
messages: [],
}
componentDidMount() {
this.setState({
messages: [
{
_id: 1,
text: 'Hello developer',
createdAt: new Date(),
user: {
_id: 2,
name: 'React Native',
avatar: 'https://placeimg.com/140/140/any',
},
},
],
})
}
onSend(messages = []) {
this.setState(previousState => ({
messages: GiftedChat.append(previousState.messages, messages),
}))
}
render() {
return (
<GiftedChat
messages={this.state.messages}
onSend={messages => this.onSend(messages)}
user={{
_id: 1,
}}
/>
)
}
}
类示例扩展了React.Component{
状态={
信息:[],
}
componentDidMount(){
这是我的国家({
信息:[
{
_id:1,
文本:“你好,开发者”,
createdAt:新日期(),
用户:{
_id:2,
名称:“反应本机”,
阿凡达:'https://placeimg.com/140/140/any',
},
},
],
})
}
onSend(消息=[]){
this.setState(previousState=>({
消息:giftechat.append(previousState.messages,messages),
}))
}
render(){
返回(
this.onSend(messages)}
使用者={{
_id:1,
}}
/>
)
}
}
\u id:1
是消息的id,\u id:2
是编写消息的用户的id,这样GiftedChat
将能够将消息与编写消息的用户绑定
使用类似的逻辑,用户发送的每条消息都将与呈现函数中的id绑定。如果是这种情况,那么在return()语句中写入的_id:1是什么,它与setState()中的return()语句_id和用户块中的_id有何不同。返回的是设备用户的id,例如,您的用户将登录到应用程序,然后您将从数据库获取他的id,然后在返回函数中使用此id向数据库发送新消息