简体   繁体   English

使用 ReactJS 存储和编辑数据

[英]Store and edit data using ReactJS

I am building an application using ReactJS.我正在使用 ReactJS 构建一个应用程序。 I am trying to find out how to store data and to edit it.我试图找出如何存储数据并对其进行编辑。 I tried to store it on my computer with 'fs, 'browserify-fs' but it didn't work.我试图用'fs,'browserify-fs'将它存储在我的电脑上,但没有用。 Should I use express, or is there any other alternatives ?我应该使用快递,还是有其他选择?

If you are using React you are operating in the browser.如果您使用 React,则您是在浏览器中操作。 Your option for storage is in local storage.您的存储选项是本地存储。 This is explained here .这是解释here

Examples of code are:代码示例如下:

// setter
localStorage.setItem('myData', data);
// getter
localStorage.getItem('myData');
// remove
localStorage.removeItem('myData');
// remove all
localStorage.clear();

Note this is stored in the browser and can be easily cleared.请注意,这存储在浏览器中,可以轻松清除。 You are going to realize that you need a back end solution.您将意识到您需要一个后端解决方案。 This is a server you can send requests to which has an API (a place you send requests to) which executes some form of operation (normally CRUD - Create Read Update Delete via a REST endpoint or GRAPHQL) to serve you back the data you are requesting from a database (MySQL, Postgres, MongoDB).这是一个您可以向其发送请求的服务器,该服务器具有一个 API(您向其发送请求的地方),该 API 执行某种形式的操作(通常是 CRUD - 通过 REST 端点或 GRAPHQL 创建读取更新删除)来为您提供数据从数据库(MySQL、Postgres、MongoDB)请求。 This is a whole different discussion.这是一个完全不同的讨论。

To store an array in local storage you will need to make it a string via JSON.stringify .要将数组存储在本地存储中,您需要通过JSON.stringify将其JSON.stringify字符串。 An example would be:一个例子是:

localStorage.setItem("array", JSON.stringify(array));

In developer tools in Chrome you can go to Application -> Storage -> Local Storage and see what is saved.在 Chrome 中的开发人员工具中,您可以转到应用程序 -> 存储 -> 本地存储并查看保存的内容。 Here is an example:下面是一个例子:

在此处输入图片说明

If you want to share the data along multiple clients you should use server-side solution or if you just want to save the data for a client only you could use client-side solution provided by @diesel .如果要沿多个客户端共享数据,则应使用服务器端解决方案,或者如果只想为客户端保存数据,则可以使用@diesel提供的客户端解决方案

Create your own web-server创建自己的网络服务器

You need to create web server and a database to store your data.您需要创建 Web 服务器和一个数据库来存储您的数据。 Database is used to store data.数据库用于存储数据。 You could use: MySQL, PostgreSQL, SQLite3, MongoDB, ... You also need to create web service to make secure database calls.您可以使用:MySQL、PostgreSQL、SQLite3、MongoDB、... 您还需要创建 Web 服务来进行安全的数据库调用。

客户端-服务器-数据库方案

To create web server you could use Express.js to write your web server easily.要创建 Web 服务器,您可以使用 Express.js 轻松编写您的 Web 服务器。

Headless Content Management Systems (abbr: CMS) Headless 内容管理系统(缩写:CMS)

If you don't want to spent time on creating your own web-server you could install a headless CMS to read/write your data using api endpoints provided by CMSs.如果您不想花时间创建自己的网络服务器,您可以安装一个无头 CMS 来使用 CMS 提供的 api 端点读取/写入您的数据。 Here's list of headless CMS softwares: headlesscms.org .以下是 headless CMS 软件列表: headlesscms.org I tried strapi which has lots of features you might need.我尝试了Strapi ,它具有许多您可能需要的功能。

Here's some strapi features:这是一些 Strapi 功能:

  • Open-source开源
  • Model builder模型构建器
  • Extensible (plugin support)可扩展(插件支持)
  • Content editor (eg: to edit articles)内容编辑器(例如:编辑文章)
  • and many more还有很多

Firebase火力基地

If you don't want to spend your time on installing CMS software to your server and maintaining it regularly you could use Database service provided by Google Firebase .如果您不想花时间在服务器上安装 CMS 软件并定期维护它,您可以使用Google Firebase 提供的数据库服务 It is also feature rich too.它的功能也很丰富。 Here's some features supported by Firebase.以下是 Firebase 支持的一些功能。

  • NoSQL Database (to store your data) NoSQL数据库(用于存储您的数据)
  • Authentication (to authenticate users)身份验证(对用户进行身份验证)
  • Storage (to store files)存储(存储文件)
  • Functions (to write serverless functions )函数(编写无服务器函数
  • Machine Learning机器学习
  • and many more还有很多

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

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