Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
275 views
in Technique[技术] by (71.8m points)

node.js - React how to listen to database for changes

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...