Answer the question
In order to leave comments, you need to log in
How to move local state in redux?
Hello. I am new to redux and also new to react. Please tell me how to transfer the local state useState to redux. In short, you need to create an action to write messages to an array, create a reducer so that it writes data about the message to the store...
import React, { useState, useEffect } from "react";
import queryString from "query-string";
import io from "socket.io-client";
import "./Chat.css";
import InfoBar from "../InfoBar/InfoBar";
import Input from "../Input/Input";
import Messages from "../Messages/Messages";
import SideBar from "../SideBar/SideBar";
import FetchRandomUser from "./FetchRandomUser";
let socket;
const Chat = ({ location }) => {
const [name, setName] = useState("");
const [room, setRoom] = useState("");
const [users, setUsers] = useState('');
const [message, setMessage] = useState("");
const [messages, setMessages] = useState([]);
const ENDPOINT = "localhost:5000";
useEffect(() => {
const { name, room } = queryString.parse(location.search);
socket = io(ENDPOINT);
setName(name);
setRoom(room);
socket.emit('join', { name, room }, (error) => {
if(error) {
alert(error);
}
});
}, [ENDPOINT, location.search]);
useEffect(() => {
socket.on('message', message => {
setMessages(messages => [ ...messages, message ]);
});
socket.on("roomData", ({ users }) => {
setUsers(users);
});
}, []);
const sendMessage = (event) => {
event.preventDefault();
if (message) {
socket.emit("sendMessage", message, () => setMessage(""));
}
};
return (
<div>
<div className="chat-container">
<InfoBar room={room} />
<div className="chat-main">
<SideBar users={users} room={room}/>
<Messages messages={messages} name={name}/>
</div>
<Input message={message} setMessage={setMessage} sendMessage={sendMessage}/>
</div>
<div className="ourUsers">
<h1>Наши пользователи</h1>
<div className="items">
<FetchRandomUser />
<FetchRandomUser />
<FetchRandomUser />
<FetchRandomUser />
</div>
</div>
</div>
);
};
export default Chat;
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question