[英]How to update new messages in a Chat App in React?
In a React Chat App, I want to display Sender's as well Reciever's messages.在 React 聊天应用程序中,我想显示发件人和收件人的消息。 I have also displayed message only on submit, but, I want it to store it in sessionStorage and also other messages should be displayed.我也只在提交时显示消息,但是,我希望它将它存储在 sessionStorage 中,并且还应该显示其他消息。 Problem is that other messages are getting updated on same message bubble.问题是其他消息正在同一消息气泡上更新。 What could be appropriate solution?什么可能是合适的解决方案?
MessageApp消息应用
const [textValue, setTextValue] = useState("");
const [newTextValue, setNewTextValue] = useState("");
const [showSenderMessage, setShowSenderMessage] = useState(false)
const [showRecieverMessage, setShowRecieverMessage] = useState(false)
const sendMessage = (e) => {
e.preventDefault();
setShowSenderMessage(true)
if (textValue != "") {
const newTextValueHere = textValue;
setNewTextValue(newTextValueHere);
setTextValue("");
} else {
return;
}
};
return (
<>
{
showSenderMessage ? (
<div
className="bubble-sender"
style={{
display: "flex",
flexWrap: "wrap",
justifyContent: "flex-start",
width: "80%"
}}
>
<span style={{ width: "20%" }}>
<img
src="https://cdn.business2community.com/wp-content/uploads/2017/08/blank-profile-picture-973460_640.png"
style={{
height: "50px",
width: "50px",
border: "2px solid black",
borderRadius: "50%"
}}
/>
</span>
<span style={{ width: "80%" }}>
{newTextValue}
<br />
<span
style={{
display: "flex",
flexWrap: "wrap",
justifyContent: "flex-end"
}}
>
<small style={{ color: "grey", float: "right" }}>11:23 AM</small>
</span>
</span>
</div>
) : null
}
<span>
<form
style={{
position: "fixed",
bottom: "0",
marginBottom: "80px",
width: "100%"
}}
>
<div className="col-lg-10 mb-3">
<div className="input-group mycustom">
<input
value={textValue}
type="text"
required
placeholder="Send Message"
maxLength="30"
onChange={(e) => setTextValue(e.target.value)}
/>
<div className="input-group-prepend">
<button
type="submit"
style={{
color: "white",
display: "flex",
flexWrap: "wrap",
justifyContent: "space-evenly"
}}
onClick={sendMessage}
>
Send Message
</button>
</div>
</div>
</div>
</form>
</span>
</>
);
Here is the codesandbox link for much clarity --> https://codesandbox.io/s/upbeat-montalcini-bpdsp这是为了清楚起见的代码框链接-> https://codesandbox.io/s/upbeat-montalcini-bpdsp
You can create an array of text messages and render them within the map.您可以创建文本消息数组并在 map 中呈现它们。 See below example.请参见下面的示例。
const [textValue, setTextValue] = useState('');
const [textMessages, setTextMessages] = useState([]);
const [showSenderMessage, setShowSenderMessage] = useState(false);
const [showRecieverMessage, setShowRecieverMessage] = useState(false);
const sendMessage = (e) => {
e.preventDefault();
setShowSenderMessage(true);
if (textValue !== '') {
setTextMessages([
...textMessages,
textValue,
]);
setTextValue('');
}
};
return (
<>
{showSenderMessage && textMessages.map((text) => (
<div
className="bubble-sender"
style={{
display: 'flex',
flexWrap: 'wrap',
justifyContent: 'flex-start',
width: '80%'
}}
>
<span style={{ width: '20%' }}>
<img
alt="blank profile"
src="https://cdn.business2community.com/wp-content/uploads/2017/08/blank-profile-picture-973460_640.png"
style={{
height: '50px',
width: '50px',
border: '2px solid black',
borderRadius: '50%'
}}
/>
</span>
<span style={{ width: '80%' }}>
{text}
<br />
<span
style={{
display: 'flex',
flexWrap: 'wrap',
justifyContent: 'flex-end'
}}
>
<small style={{ color: 'grey', float: 'right' }}>11:23 AM</small>
</span>
</span>
</div>
))}
<span>
<form
style={{
position: 'fixed',
bottom: '0',
marginBottom: '80px',
width: '100%'
}}
>
<div className="col-lg-10 mb-3">
<div className="input-group mycustom">
<input
value={textValue}
type="text"
required
placeholder="Send Message"
maxLength="30"
onChange={(e) => setTextValue(e.target.value)}
/>
<div className="input-group-prepend">
<button
type="submit"
style={{
color: 'white',
display: 'flex',
flexWrap: 'wrap',
justifyContent: 'space-evenly'
}}
onClick={sendMessage}
>
Send Message
</button>
</div>
</div>
</div>
</form>
</span>
</>
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.