When working with databases, what is the good practise to make a react component auto-update with new values? I am using Express and MangoDB
class Container extends Component {
constructor(props) {
super(props);
this.state = {
show: false,
documents: [],
};
}
//fetch all the documents from database and display it
getDoc = () => {
axios.get(API).then( (response) => {
let documents = response.data.map((doc) => {
return (
<div>{document.value}</div>
)
})
})
}
//create a new document into the database
createDoc = () =>{
axios.post(API, {value: 'new value'}).then( (response) => {
this.getDoc(); //how to improve this
})
}
}
I tried setState() but not re-render the component
question from:
https://stackoverflow.com/questions/65837773/react-how-to-listen-to-database-for-changes 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…