Jos*_*osh 3 javascript reactjs redux
我有一个使用 Redux 的 React 应用程序,出于测试目的,我创建了一个本地 JSON 文件,该文件位于data从我的应用程序内调用的文件夹内。
我试图将该 JSON 数据传递给 Redux,以便我可以访问它,然后使用它在单独的组件中映射和显示项目,但我不知道如何执行此操作。任何人都可以提供有关如何实现此目标的示例或资源吗?
正如用户Sreeram在评论中所说,您可以直接require或importjson 文件,之后您将可以像任何其他对象一样访问它。
这意味着您可以使用它来初始化您的减速器中的状态。
const testData = require('./data/thejson.json');
// use testData as the default argument, which will be used if state is undefined (as it will be when the app starts
function someReducer(state = testData , action) {
switch (action.type) {
// some code here..
}
}
Run Code Online (Sandbox Code Playgroud)