简体   繁体   English

在reactjs中使用useReducer从文件中获取数据的问题

[英]problem in fetching data from a file by using useReducer in reactjs

I am new to reactjs and playing with useState and useReducer.我是 reactjs 的新手,正在使用 useState 和 useReducer。 I have data in a file and from that file I want to get data.我在文件中有数据,我想从该文件中获取数据。 I can do it with useState but when I want to do it by using useReducer I get an error.我可以用 useState 来做,但是当我想用 useReducer 来做时,我得到一个错误。 Another interesting thing is that, if I insert that same data in useReducer's initial state it works and displays the data.另一个有趣的事情是,如果我在 useReducer 的初始状态中插入相同的数据,它会工作并显示数据。 Below is the code and data file.下面是代码和数据文件。

dataOnFile.js (file from which I want to fetch data) dataOnFile.js(我想从中获取数据的文件)

const dataOnFile = [
{
    id: 1,
    name: "ahmad",
    status: true,
},
{
    id: 2,
    name: "john",
    status: true,
},
{
    id: 3,
    name: "sara",
    status: true,
},
];
export default dataOnFile;

globalContext.js (using context to use it in other compnents, in this I am using useState and by using useState it works fine, it accepts the Items as an initial state) globalContext.js(使用上下文在其他组件中使用它,在这里我使用 useState 并且通过使用 useState 它工作正常,它接受 Items 作为初始状态)

import React, { createContext, useReducer, useState } from "react";
import Items from "./dataOnFile";

export const GlobalContext = createContext();

export function GlobalProvider(props) {
const [items, setItems] = useState(Items);
return (
    <div>
        <GlobalContext.Provider value={[items, setItems]}>
            {props.children}
        </GlobalContext.Provider>
    </div>
);
}

GlobalContext.js (by using useReducer, I get an error, it's not accepting Items as an initial state) ReferenceError: Cannot access 'Items' before initialization GlobalContext.js(通过使用 useReducer,我得到一个错误,它不接受 Items 作为初始状态) ReferenceError:无法在初始化之前访问“Items”

import React, { createContext, useReducer, useState } from "react";
import Items from "./dataOnFile";

export const GlobalContext = createContext();

function itemsReducer(Items, action) {
return <div></div>;
}

export function GlobalProvider(props) {
const [Items, itemsDispatch] = useReducer(itemsReducer, Items);

return (
    <div>
        <GlobalContext.Provider value={[Items, itemsDispatch]}>
            {props.children}
        </GlobalContext.Provider>
    </div>
);
}

globalContext.js (if I put data on useReducer's initial state it works) globalContext.js(如果我将数据放在 useReducer 的初始状态,它会起作用)

import React, { createContext, useReducer, useState } from "react";
import Items from "./dataOnFile";

export const GlobalContext = createContext();

function itemsReducer(Items, action) {
return <div></div>;
}

export function GlobalProvider(props) {
const [Items, itemsDispatch] = useReducer(itemsReducer, [
    {
        id: 1,
        name: "ahmad",
        status: true,
    },
    {
        id: 2,
        name: "sheeraz",
        status: true,
    },
]);

return (
    <div>
        <GlobalContext.Provider value={[Items, itemsDispatch]}>
            {props.children}
        </GlobalContext.Provider>
    </div>
);
}

I think the problem here is that you're putting Items as the state for the reducer, the initial state and the parameter in the reducer.我认为这里的问题是您将Items作为减速器的状态、初始状态和减速器中的参数。 Note that useReducer calls the hook, thats where you want to pass your initial state.请注意, useReducer 调用钩子,这是您想要传递初始状态的地方。

Try this in the component:在组件中试试这个:

const [items, itemsDispatch] = useReducer(itemsReducer, Items);

(Note that there is no capital 'I' in that state name) (请注意,该州名中没有大写“I”)

And this in the reducer:这在减速器中:

const itemsReducer = (state, action) => {
// Do stuff
}

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

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