0

Has anyone made a react website with static data that does not have a backend to store information, but needs to add a query/search functionality in the front end that can fetch information(a specific keyword that can be present on multiple pages of the app) from the entire app?

What I am looking for is, an option to store the data and made a query from there without implementing a backend from scratch. Is that possible? From my research so far, what I have found so far is I need to store all the information to some storage services like Firebase or Firestore and need to retrieve data from the entire app and only thenI can implement search functionality using third-party apps like typesense or algolia. But did not find any specific example based on that. I am a beginner in react, so any kind of help would be highly appreciated.

8
  • Do you mean something like a built-in JSON data file within the app that can be imported into a component to be referenced/searched/filtered/etc? Commented Jul 31, 2022 at 22:26
  • You can easily store data just as a mydata.json file and then load it using fetch. If you load the json in the root App component, it will be available in the whole app. Firebase and such is only needed if you want to manipulate the data and then store it on the server again. Commented Jul 31, 2022 at 22:31
  • @Drew Reese Say, for example, I have a paragraph tag like this in several pages of the app (<p>Lorem ipsum dolor sit amet</p>) where I am hardcoding the information without fetching data from a backend server or database and I want to find all the pages that has the keyword "Lorem" in there by typing "Lorem" in the search input located in my home page, then how can I achieve them? Commented Jul 31, 2022 at 22:38
  • @Kokodoko, thanks for the information. Just to clarify, should I store data of all the components that I have in my app in the same JSON file and fetch it from there? Commented Jul 31, 2022 at 22:42
  • You want to search your app's rendered output for specific key words? If all the content is hardcoded then I suppose you'll be searching your source code. If the content is stored in a JSON then you only need to search that. BTW, you don't need to fetch the JSON file if it's a local asset, you can just import it normally. It's much simpler than setting up the lifecycle to fetch it and handle storing the response. Commented Jul 31, 2022 at 22:46

1 Answer 1

1

You can use Local Storage API to save data on the frontend. You can store a JSON object as string in the localstorage using

localStorage.setItem('my-key', jsonObjectString);

Later you can retrieve the object using

const textFromStorage = localStorage.getItem('my-key');
let jsonObject = JSON.parse(textFromStorage)
// Now you can do your query here
Sign up to request clarification or add additional context in comments.

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.