I am developing a React.js-Express.js website, and I had set up some basic code with the help of an online example. I had Express.js send an array to the frontend to display it after parsing. However, when I changed the array just a little – literally changed a string to another string – my frontend did not update.
Express – users.js file
JavaScript
x
17
17
1
var express = require('express');
2
var router = express.Router();
3
4
/* GET users listing. */
5
router.get('/', function(req, res, next) {
6
//res.send('respond with a resource');
7
8
res.json([{
9
id: 1,
10
username: "bye" //I changed this string (used to be "samsepi0l")
11
}, {
12
id: 2,
13
username: "hi" //And this string (used to be "D0loresH4ze")
14
}]);
15
});
16
17
module.exports = router;
React – About.js file
JavaScript
1
28
28
1
import React from 'react'
2
// import { Link } from 'react-router-dom'
3
import { Header } from './Header'
4
5
export class About extends React.Component {
6
state = {users: []}
7
8
componentDidMount() {
9
fetch('/users')
10
.then(res => res.json())
11
.then(users => this.setState({ users }));
12
}
13
14
render() {
15
return (
16
<div>
17
<Header />
18
<h2 id="other_pages_h2">About</h2>
19
<div>
20
<h1>Users</h1>
21
{this.state.users.map(user =>
22
<div key={user.id} style={{color: 'white'}}>{user.username}</div>
23
)}
24
</div>
25
</div>
26
)
27
}
28
}
For some reason, the /about
page still displays “samsepi0l” and “D0loresH4ze”. How can I fix this?
Advertisement
Answer
This is likely caused by the infamous cache. Usually just hitting Crtl + F5 will fix this, if that doesn’t work, clear your browser history.
If you’re still running into the same problem, then you didn’t save the file and/or restart the server since the code change.