1

I´m developing an small test app and I had a doubt about JSON in frontend.

I´m working with React and I´m using a JSON file as a small testing DB, saving only 1 user. The fact is that I want to write more users there and edit the JSON registers.

Is that possible without backend or AJAX?

**EDIT: **

I´m using localStorage for the actual logged in user and it´s moves, at the end need to save it´s data before I close session and localStorage loses.

6
  • 1
    Without Ajax, yes, backend no Commented Mar 10, 2021 at 13:15
  • There are numerous cloud data storage services you can use Commented Mar 10, 2021 at 13:19
  • When you close the browser or the tab, the data inside localStorage should not be lost. Be sure to use localStorage and not sessionStorage Commented Mar 10, 2021 at 13:22
  • Oh true, I missed something in the edit, when I close session I clean the localStorage data, using it as a login "flag" Commented Mar 10, 2021 at 13:25
  • @charlietfl the fact is that I need only front-end without external services Commented Mar 10, 2021 at 13:26

2 Answers 2

1

AJAX is used for HTTP requests between a front-end (ex: ReactJS, VueJS) and a back-end (ex: NodeJS/Express, Python/Flask).

If you're using a JSON variable, the data will be deleted everytime the website refreshes. If you're using a JSON file, you can't access the file system from the browser for security reasons.

What I recommend to use is localStorage, which acts as a local database for front-end ONLY per browser (per user). The most common usage is:

The setter:

localStorage.setItem('name', 'John')

The getter:

const name = localStorage.getItem('name');
console.log(name) // John

In React, you could set and update data as the following in this example I made:

DEMO https://stackblitz.com/edit/react-fctts3

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for the answer @josemartindev, the fact is that I should use Redux for this things, but honestly I don´t know how to use it (need to study it). The fact is that I´m only developing frontend on this test and I need something to save the data, create new users and modify their values.
Before learning Redux, try first using React's state + localStorage. When you modify state, save in localStorage as well. Be sure to set state and set localStorage before entering components as well (using useEffect or componentDidMount)
The fact is what I´ve updated in my question, I´m using localStorage, but I need to create more users and modify their datas (something simple, plain data) I´m also using states but I didn´t get it at all .
@B2DAW When you say "Save data before I close session", to save this data you actually NEED a back end in order to save the data in a JSON file or in a Database, because with React you can't access the file system.
@josemartindev you´re right, I will save it all in localStorage, is the easiest solution, thanks man!
|
1

Short answer, no. you can't write data directly to client's disk. but there are workarounds. one way is using common browsers storage options. such as localstorage:

localStorage.setItem(keyname, value)

read more here

another way if you need to move the data is to make the user download your file, and save it. when you need it again they can re-upload it to your front-end page. you can read up on this here.

of course there are other options like cookies and IndexedDB/WebSQL. but I think local storage can do just fine for your needs.

EDIT: if you don't have problem with having an internet connection, there is also the awesom option of using google sheets as a database! using this tool: https://sheety.co/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.