0

I want to create some kind of temporary "database" for my test project.

What is the best solution to store those objects inside React app and how to access it in different components?

To clear what I mean, I've got a search bar, after enter is pressed the function would go through the "database" and if database.title is equal to user input a new div would be rendered with object details after a button is clicked.

I would be grateful for pointing me in the right direction.

1 Answer 1

1

You can create a dummy data file containing an array or some sort of data in it and then import it in which component you want to test.

Example:

// dummyData.js
const dummyData = [
    {
       title: 'this is a title',
       type: 'book'
    },
    {
       title: 'this is another title',
       type: 'movie'
    },
    {
       title: 'this is a foo',
       type: 'bar'
    }
];
export default dummyData;

// on your component
import dummyData from '/path/to/your/dummyData.js';
Sign up to request clarification or add additional context in comments.

3 Comments

So my next step after importing it should be something like: After clicking enter key function would iterate through the database with for(var i=0; i < database.length; i++){ if({database.name} === {this.state.term}) //term being the search term and if it finds a match I would render a new div with the database item Am I correct?
Yes. You can use some library like lodash or underscore for complicated iterations. Of course this solution is for only development and testing purposes.
Okay, thank you, I will play with this idea for a while

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.