简体   繁体   English

在ReactJS中实现javascript / json对象

[英]Implementing javascript / json objects in ReactJS

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? 在React应用程序中存储这些对象的最佳解决方案是什么?如何在不同组件中访问它们?

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. 为了清楚我的意思,我有一个搜索栏,按下Enter键后,该功能将通过“数据库”,如果database.title等于用户输入,则在按下按钮后将显示一个带有对象详细信息的新div。点击。

I would be grateful for pointing me in the right direction. 我很高兴为我指出正确的方向。

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';

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

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