简体   繁体   中英

Pattern for a React component with access to lowdb

I am looking for a pattern for a react component. It should include lowdb. Someone already build one?

Okay, I dont access the lowdb by frontend. I use Axios to send a request to my node/express backend and respond with the lowdb-data. Requires you know how to interact with a react frontend and a node/express backend on different ports. I dont let express render my react-app... looks something like this and axios as a dependency in the package.json + import Axios from 'axios':

export default class Status extends Component {
  constructor(props) {
    super(props);
    this.state = "";
  }
  componentWillMount() {
    Axios.get("http://localhost:4000/dbUserAuth").then((res) => {
      let authState = res.data;
      if (authState) {
        this.setState({ authState });
      }
    });
  }

This sends a request to my node.js/express backend which runs on port 4000. The frontend runs on 2000. The backend can look something like this:

server.get("/dbUserAuth", (request, response) => {
  function resAuthData() {
    let array = [];
    const dbUserAuth = db.get("UserAuth[0]").value();
    const dbChatIdFound = db.get("UserAuth[1]").value();
    const botActive = db.get("UserAuth[2]").value();
    array.push(dbUserAuth, dbChatIdFound, botActive);
    response.send(array);
  }
  resAuthData();
});

Hopefully someone needs theese snippets. best regards

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM