我每x毫秒从节点服务器发送一个哑数据。
我希望这些数据能够实时显示。
问题在于,初始化后Tile的组件不会更改
我还创建了一个可单击的图块,因此,每当单击该窗口时,都会出现一个模态窗口,在那里将更新数据。
我已经读到,只要您设置了状态,就将重新渲染调用,但这不会影响tile组件 updated information
节点服务器:
const express = require("express");
const http = require("http");
const socketIo = require("socket.io");
const app = express();
const server = http.createServer(app).listen(5001);
const io = socketIo(server);
io.set('origins', 'http://localhost:3000');
let today = new Date();
let time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();
let data ={
"news" :
{ "short" :'Hello short news ' + time,
"long" : 'Hello long news ' + time
},
"weather" : {
"short" : 'really hot today ' + time,
"long" : 'drink a lot ' + time
}
};
io.on('connection', function(socket){
console.log('connected');
socket.emit('initial_data',data);
socket.on('data',() => {
socket.emit('get_data', data);
});
setInterval(function() {
today = new Date();
time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();
let newsUpdate ={
"news" :
{ "short" :'Hello short news ' + time,
"long" : 'Hello long news ' + time
}
};
socket.emit('news_update', newsUpdate);
console.log('Last updated: ' + today);
}, 400); //600000 is 10 min
setInterval(function() {
today = new Date();
time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();
let newsUpdate = {"weather" : {
"short" : 'really hot today ' + time,
"long" : 'drink a lot ' + time
}};
socket.emit('weather_update', newsUpdate);
console.log('Last updated: ' + time);
}, 400); //600000 is 10 min
});
module.exports = app;
反应ui: 仪表板
import React from 'react';
import {Modal} from 'react-bootstrap'
import {ButtonBase,Grid,Card,CardContent} from '@material-ui/core';
import socketIOClient from 'socket.io-client';
import {Tile} from './Tile';
export class DashBoard extends React.Component{
showModal = (card) => {
this.setState({ [card]: true});
};
hideModal = (card) => {
this.setState({ [card]: false });
};
constructor(props) {
super(props);
this.showModal = this.showModal.bind(this);
this.hideModal = this.hideModal.bind(this);
this.state = {
showWeather: false,
showNews:false,
response:false,
news_response: false,
weather_response:false,
endpoint: "http://localhost:5001"
};
}
componentDidMount() {
const {endpoint} = this.state;
const socket = socketIOClient(endpoint);
socket.on('initial_data', (data) => {
this.setState({news_response: data["news"] ,weather_response :data["weather"] });
console.log(this.state);
});
socket.on("news_update", data => {this.setState({news_response: data["news"]});});
socket.on("weather_update", data => {this.setState({weather_response: data["weather"]});});
this.forceUpdate();
}
render(){
if(this.state.weather_response && this.state.news_response)
return(
<>
<Grid
container
direction="column"
justify="center"
alignItems="center">
<Card>
<CardContent>
<ButtonBase onClick={() => this.showModal('showNews')}>
<Tile title={"Lets get some news here!"} short={this.state.news_response["short"]}></Tile>
</ButtonBase>
</CardContent>
</Card>
<Card>
<CardContent>
<ButtonBase onClick={() => this.showModal('showWeather')}>
<Tile title={"Lets get weather conditions here!"} short={this.state.weather_response["short"]}> </Tile>
</ButtonBase>
</CardContent>
</Card>
</Grid>
<Modal show={this.state.showNews} onHide={()=>this.hideModal('showNews')}>
<Modal.Header closeButton> </Modal.Header>
<Modal.Body> <Tile title={"Lets get some news here!"} short={this.state.news_response["short"]} long={this.state.news_response["long"]}> </Tile> </Modal.Body>
</Modal>
<Modal show={this.state.showWeather} onHide={()=>this.hideModal('showWeather')}>
<Modal.Header closeButton></Modal.Header>
<Modal.Body> <Tile title={"Lets get weather conditions here!"} short={this.state.weather_response["short"]} long={this.state.weather_response["long"]}> </Tile></Modal.Body>
</Modal>
</>
);
else
return(<>Loading...</>)
}
}
瓷砖组件:
import React from 'react';
export class Tile extends React.Component{
constructor(props){
super(props);
this.state={title:this.props.title,
short: this.props.short ,
long:this.props.long};
}
render(){
return(
<div>
<h1>{this.state.title}</h1>
<p>{this.state.short}</p> <br></br>
{this.state.long}
</div>
);
}
}
答案 0 :(得分:0)
根据此帖子 https://medium.com/@hugoleon46/for-anyone-that-needs-this-functionality-a-little-update-7b7bdbe8cc1f
通过添加Tile组件来解决:
static getDerivedStateFromProps(props, state) {
if (props.short !== state.short || props.long !== state.long ) {
return { short: props.short , long: props.long };
}
return null;
}