[英]React Chat app works fine in Production but not in Development (Chat App) | React (with Materia-ui), Socket.io, Express
When I'm viewing the build mode of the app it doesn't update the chat and looks to render out certain areas twice as when I put a console.log within the functional component in store.js it outputs the log twice but doesn't actually update the chat.当我查看应用程序的构建模式时,它不会更新聊天并且看起来会渲染某些区域两次,因为当我将 console.log 放在 store.js 的功能组件中时,它会输出日志两次但没有t 实际上更新聊天。
However, when I check the build mode of the chat it works fine and doesn't have any issues.但是,当我检查聊天的构建模式时,它工作正常并且没有任何问题。 I've no idea what is happening, there's no strict mode anywhere so I can't see what could be causing it我不知道发生了什么,任何地方都没有严格模式,所以我看不出是什么原因造成的
Client Side Handling客户端处理
import React, { useReducer } from "react";
import io from 'socket.io-client'
export const CTX = React.createContext();
const initState = {
General: [
{from: 'Jack', msg: 'Epic'},
{from: 'John', msg: 'Totally Epic'},
{from: 'James', msg: 'Amazing'},
],
Topic1: [
{from: 'Jack', msg: 'damn daniel'},
{from: 'John', msg: 'dead meme '},
{from: 'Jacob', msg: 'lmao'},
],
};
export const RECEIVE_MESSAGE = "RECEIVE_MESSAGE";
function reducer(state, action) {
const { from, msg, topic } = action.payload;
switch (action.type) {
case "RECEIVE_MESSAGE":
return {
...state,
[topic]: [
...state[topic],
{
from,
msg,
},
],
};
default: {
return state;
}
}
}
let socket;
function sendChatAction(value){
socket.emit('chat message', value);
console.log(value);
}
export default function Store(props) {
const [state, dispatch] = useReducer(reducer, initState);
if(!socket){
socket = io(':3001')
socket.on('chat message', function(msg){
dispatch({ type: "RECEIVE_MESSAGE", payload: msg });
});
}
const user = 'Player' + Math.random(100).toFixed(2);
console.log({ state });
return (
<CTX.Provider value={{ state, sendChatAction, user }}>
{props.children}
</CTX.Provider>
);
};
React Component反应组件
import React from "react";
import { makeStyles } from '@material-ui/core/styles';
import Paper from '@material-ui/core/Paper';
import Typography from '@material-ui/core/Typography';
import List from '@material-ui/core/List';
import ListItem from '@material-ui/core/ListItem';
import ListItemText from '@material-ui/core/ListItemText';
import Chip from '@material-ui/core/Chip';
import TextField from '@material-ui/core/TextField';
import Button from '@material-ui/core/Button';
import {CTX} from './Store'
const useStyles = makeStyles((theme) => ({
root: {
margin: '50px',
padding: theme.spacing(3, 2),
textAlign: 'center',
},
flex: {
display: 'flex',
alignItems: 'center',
},
topicsWindow: {
width: '30%',
height: '300px',
borderRight: '1px solid grey',
},
chatWindow: {
width: '70%',
height: '300px',
padding: '20px',
},
chatBox: {
width: '85%',
},
button: {
width: '15%',
}
}));
export default function Dashboard(){
const classes = useStyles();
//CTX store
const {state, sendChatAction, user} = React.useContext(CTX);
const topics = Object.keys(state);
//localstate
const [activeTopic, changeActiveTopic] = React.useState(topics[0])
const [textValue, changeTextValue] = React.useState('');
return(
<div>
<Paper className={classes.root}>
<Typography variant="h3" component="h3">
Chat App
</Typography>
<Typography variant="h5" component="h5">
{activeTopic}
</Typography>
<div className={classes.flex}>
<div className={classes.topicsWindow}>
<List>
{
topics.map(topic => (
<ListItem onClick={e => changeActiveTopic(e.target.innerText)} key={topic} button>
<ListItemText primary={topic} />
</ListItem>
))
}
</List>
</div>
<div className={classes.chatWindow}>
{
state[activeTopic].map((chat, i) => {
return (
<div className={classes.flex} key={i}>
<Chip label={chat.from} className={classes.chip} />
<Typography variant='body1' gutterBottom>
{chat.msg}
</Typography>
</div>
)
})
}
</div>
</div>
<div className={classes.flex}>
<TextField
label="Send a chat"
className={classes.chatBox}
value={textValue}
onChange={e => changeTextValue(e.target.value)}
margin="normal"
/>
<Button
variant="contained"
color="primary"
onClick={() => {
sendChatAction({
from: user,
msg: textValue,
topic: activeTopic
});
changeTextValue('');
}}>
Send
</Button>
</div>
</Paper>
</div>
);
}
Server服务器
var app = require('express')();
var http = require('http').createServer(app);
var io = require('socket.io')(http);
io.on('connect', function(socket) {
console.log('user connected', socket.id);
socket.on('chat message', (msg) => {
console.log('message: ' + JSON.stringify(msg));
io.emit('chat message', msg)
});
});
http.listen(3001, function() {
console.log('listening on * : 3001');
});
I've been reading through the documentation for reducer function and react hooks but I can't see what it might be我一直在阅读减速器 function 和反应钩子的文档,但我看不出它可能是什么
I'm answering my own question rather than deleting so future people can see.我正在回答我自己的问题,而不是删除以便未来的人们可以看到。
remove strict mode from the index.js file in src.从 src 中的 index.js 文件中删除严格模式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.