简体   繁体   English

可访问 lowdb 的 React 组件的模式

[英]Pattern for a React component with access to lowdb

I am looking for a pattern for a react component.我正在寻找反应组件的模式。 It should include lowdb.它应该包括lowdb。 Someone already build one?有人已经建好了?

Okay, I dont access the lowdb by frontend.好的,我不通过前端访问 lowdb。 I use Axios to send a request to my node/express backend and respond with the lowdb-data.我使用 Axios 向我的 node/express 后端发送请求并使用 lowdb-data 进行响应。 Requires you know how to interact with a react frontend and a node/express backend on different ports.要求您知道如何与不同端口上的 react 前端和 node/express 后端进行交互。 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': 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:这会向我的 node.js/express 后端发送一个请求,该后端在端口 4000 上运行。前端在 2000 上运行。后端看起来像这样:

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此致

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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