简体   繁体   English

ReactJS->如何使用数据库中的信息填充我的 state?

[英]ReactJS->How do i populate my state with the information from within a database?

So I have some JSON lists that look like this:所以我有一些看起来像这样的 JSON 列表:

const productsList=[
    {
        name: 'Product1',
        image: productImage,
        price: 36.96,
        quantity: 1,
        id: v4()
    },
    {
        name: 'Product 2',
        image: productImage,
        price: 39.94,
        quantity: 1,
        id:v4()
    },
]

That list is then shown on the page using the map function and it is also being stored in the state using mapStateToProps.然后使用 map function 在页面上显示该列表,并且它也使用 mapStateToProps 存储在 state 中。 My question is how can I fill the productsList with the data from within a sqlite database?我的问题是如何用sqlite数据库中的数据填充 productsList?

Here are the things you need:以下是您需要的东西:

  • You need a backend API service that connects to the SQLite database.您需要一个连接到 SQLite 数据库的后端 API 服务。 If you use a Node.js backend, you can use this library .如果你使用 Node.js 后端,你可以使用这个库
  • Once it's setup and running, you need to add the code to your frontend React app for fetching the data from your server.设置并运行后,您需要将代码添加到前端 React 应用程序以从服务器获取数据。 You can use hooks to achieve it.您可以使用钩子来实现它。

I can't write every line of the code that you need since it's a lot.我无法编写您需要的每一行代码,因为它很多。 Hopefully this gives you an idea.希望这能给你一个想法。

  1. You need to create an API for the JSON data and have your URL-endpoint.您需要为 JSON 数据创建 API 并拥有您的 URL 端点。
  2. Then use ** Redux-Thunk Library** to make an asynchronous API call to the URL-ENDPOINT and update your Reducer as per the API action call and hence your state in the application.然后使用 ** Redux-Thunk Library** 对 URL-ENDPOINT 进行异步 API 调用,并根据 API 操作调用更新您的Reducer ,从而更新应用程序中的 Z9ED39E2EA931586B6A985A6942EF57。

The art of creating API is what backend developers do.创建 API 的艺术是后端开发人员所做的。

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

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