[英]ERROR: undefined in React useContext and useReducer
I am trying to make a redux pattern with useContext
and useReducer
.我正在尝试使用useContext
和useReducer
制作一个 redux 模式。 I create a component that creates a context (a reducer
), and I also have the initial values.我创建了一个创建上下文的组件(一个reducer
),我也有初始值。 I declare my reduction with the foregoing, and return to the child component that is being provided by the values of my Reducer.我用上述声明我的reduce,并返回由我的Reducer的值提供的子组件。 Then in the next image I have a view that will render a component that makes a form and that is wrapped in the ExpenseReducer
component so that it has the values of my reduce.然后在下一张图片中,我有一个视图,该视图将呈现一个组件,该组件生成一个表单并包装在ExpenseReducer
组件中,以便它具有我的 reduce 的值。
In the form, I import the context and try to use the dispatch, but I get an error like "undefined"在表单中,我导入上下文并尝试使用调度,但我收到类似“未定义”的错误
My Code我的代码
import React from "react";
//Context
export const ExpenseContext = React.createContext();
// Reducer
const reducer = (state, action) => {
switch (action.type) {
case "HANDLE_SUBMIT":
return alert("Guardado");
default:
return state;
}
};
// Valores iniciales
const initialExpenses = [
{ id: "37c237f8-3004-4f69-9101-62f59ba4ce09", charge: "carne", amount: "20" },
{
id: "32bf7455-61c8-48d5-abe1-a38c93dcf1c8",
charge: "internet",
amount: "20"
},
{ id: "7e22c2e8-7965-41fe-9f39-236f266c9f24", charge: "ca", amount: "1" }
];
function ExpenseReducer({ children }) {
const [expenses, dispatch] = React.useReducer(reducer, initialExpenses);
return (
<ExpenseContext.Provider value={{ expenses, dispatch }}>
{children}
</ExpenseContext.Provider>
);
}
export default ExpenseReducer;
import React from "react";
// Components
import ExpenseForm from "../components/ExpenseForm";
import ExpenseReducer from "../reducers/ExpenseReducer/ExpenseReducer";
function ExpenseNew() {
return (
<ExpenseReducer>
<ExpenseForm />
</ExpenseReducer>
);
}
import React, { useContext } from "react";
import "./ExpenseForm.scss";
import { ThemeContext } from "../App";
import { ExpenseContext } from "../reducers/ExpenseReducer/ExpenseReducer";
const ExpenseForm = () =>
// {
// // edit,
// // charge,
// // amount,
// // handleCharge,
// // handleAmount,
// // handleSubmit
// }
{
// Theme
const { theme } = useContext(ThemeContext);
const { expenseContext } = useContext(ExpenseContext);
return (
<form
className="form"
onSubmit={expenseContext.dispatch("HANDLE_SUBMIT")}
>
<div className="form-group">
{/*To conect the value with the variable */}
<input
type="text"
className={`${theme} form-control`}
// id="charge"
// name="charge"
// placeholder="Gasto"
// value={charge}
// onChange={handleCharge}
/>
</div>
<div className="form-group">
{/*To conect the value with the variable */}
<input
type="number"
className={`${theme} form-control`}
// id="amount"
// name="amount"
// placeholder="Cuanto"
// value={amount}
// onChange={handleAmount}
/>
<textarea
placeholder="Descripción"
className={`${theme} form-control`}
id=""
cols="30"
rows="10"
></textarea>
</div>
<button type="submit" className={`btn ${theme}`}>
{true ? "Editar" : "Guardar"}
</button>
</form>
);
};
export default ExpenseForm;
Don't forget that the argument to useContext must be the context object itself:不要忘记 useContext 的参数必须是上下文对象本身:
- Correct: useContext(MyContext)正确:useContext(MyContext)
- Incorrect: useContext(MyContext.Consumer)不正确:useContext(MyContext.Consumer)
- Incorrect: useContext(MyContext.Provider)不正确:useContext(MyContext.Provider)
Your reducer is returning the provider, not the context object.您的减速器正在返回提供者,而不是上下文对象。 Hence it results in expenseContext
being undefined
.因此它导致expenseContext
是undefined
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.