Answer the question
In order to leave comments, you need to log in
How to update a component?
API node:
var express = require('express');
var router = express.Router();
var ch = 0
router.post('/', function(req, res) {
ch += 1
res.json({
ch
})
})
router.get('/c', function(req, res, next) {
res.json(
ch
)
})
module.exports = router;
import React, { Component } from 'react';
import './App.css';
class App extends Component {
state = {
count: []
}
componentDidMount() {
fetch('/users/c')
.then(res => res.json())
.then(count => this.setState({ count }));
}
render() {
return (
<div>
{this.state.count}
<button onClick={this.add}>add</button>
</div>
)
}
add = () => {
var xhr = new XMLHttpRequest();
xhr.open('POST', '/users/');
xhr.send();
}
}
export default App;
Answer the question
In order to leave comments, you need to log in
add = () => {
fetch('/users/', { method: 'POST' })
.then(res => res.json())
.then(count => this.setState({ count }));
};
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question