如何通过功能之间的const usestate(“; quot”)

发布于 2025-02-13 21:15:17 字数 1264 浏览 1 评论 0原文

我想将cont chatlog从主要功能聊天中拉出,并暂时将其插入聊天功能的组件或外部。问题是Chatlog需要在聊天功能中调用的usestate变量[msg,sendmsg](..)。无论如何我该怎么做?是新手反应。

function Chats() {

const [msg, sendMsg] = useState("");
const [msgs1, sendMsgAll] = useState([]);

useEffect(() => {

    onValue(ref(database), (snapshot) => {
    sendMsgAll([]);

    const data = snapshot.val();          
        if (data !== null) {
            Object.values(data).map((msg) => {
                sendMsgAll((oldArray) => [...oldArray, msg]);         
        });
        }
    });
}, [])

const ChatLog = () => {
    return (
    <div>
    {msgs1.map((msg) => (
        <div className="chat-log">
                <p align = {checkSide(msg.usr)}>
                <h2>{msg.msg}</h2>
                <h4>User: {msg.usr}</h4>
                <h4>Time: {convertUnix(msg.time)}</h4>
                <button>update</button>
                <button>delete</button>
                </p>
            </div>
        ))}
    </div>
    )
}

return (
    <div className="ChatView">
    <p><ChatLog /></p>
    <p>{ChatInput()}</p>
    </div>
    )

};

I would like to pull the const ChatLog out of the main function Chats and insert it as a component or outside of the Chats function for now. The Problem is that the ChatLog needs the useState variables [msg, sendMsg] (..) that are called in the Chats function. How could I do this anyway? Am new to react.

function Chats() {

const [msg, sendMsg] = useState("");
const [msgs1, sendMsgAll] = useState([]);

useEffect(() => {

    onValue(ref(database), (snapshot) => {
    sendMsgAll([]);

    const data = snapshot.val();          
        if (data !== null) {
            Object.values(data).map((msg) => {
                sendMsgAll((oldArray) => [...oldArray, msg]);         
        });
        }
    });
}, [])

const ChatLog = () => {
    return (
    <div>
    {msgs1.map((msg) => (
        <div className="chat-log">
                <p align = {checkSide(msg.usr)}>
                <h2>{msg.msg}</h2>
                <h4>User: {msg.usr}</h4>
                <h4>Time: {convertUnix(msg.time)}</h4>
                <button>update</button>
                <button>delete</button>
                </p>
            </div>
        ))}
    </div>
    )
}

return (
    <div className="ChatView">
    <p><ChatLog /></p>
    <p>{ChatInput()}</p>
    </div>
    )

};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

贱贱哒 2025-02-20 21:15:17

您可以将道具添加到Chatlog组件中。检查一下...

 const ChatLog = (props) => {
    const {msg1} = props
    return (
    <div>
    {msgs1.map((msg) => (
        <div className="chat-log">
                <p align = {checkSide(msg.usr)}>
                <h2>{msg.msg}</h2>
                <h4>User: {msg.usr}</h4>
                <h4>Time: {convertUnix(msg.time)}</h4>
                <button>update</button>
                <button>delete</button>
                </p>
            </div>
        ))}
    </div>
    )
}

但是,使用组件时需要将道具添加到组件中。这样的事情...

return (
   <div className="ChatView">
     <p><ChatLog msg1={msg1} /></p>
     <p>{ChatInput()}</p>
   </div>
)

又一件事,当您声明组件时,必须将其声明为另一个组件。像这样

//this is ChatLog component
const ChatLog = (props)=>{
   return <div/>
}

//this is Chats component
const Chats = ()=>{
   return (
      <div>
        <ChatLog {...props}/>
      </div>
  )
}

You can add props to ChatLog component. Check this...

 const ChatLog = (props) => {
    const {msg1} = props
    return (
    <div>
    {msgs1.map((msg) => (
        <div className="chat-log">
                <p align = {checkSide(msg.usr)}>
                <h2>{msg.msg}</h2>
                <h4>User: {msg.usr}</h4>
                <h4>Time: {convertUnix(msg.time)}</h4>
                <button>update</button>
                <button>delete</button>
                </p>
            </div>
        ))}
    </div>
    )
}

However,you need add props to component when you use it. Something like this...

return (
   <div className="ChatView">
     <p><ChatLog msg1={msg1} /></p>
     <p>{ChatInput()}</p>
   </div>
)

One more thing, when you declaring a component, it has to be declared of another component. Like this

//this is ChatLog component
const ChatLog = (props)=>{
   return <div/>
}

//this is Chats component
const Chats = ()=>{
   return (
      <div>
        <ChatLog {...props}/>
      </div>
  )
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文