英文:
dispatch is not a function at onClick in reactjs
问题
以下是您提供的代码的翻译部分:
CounterOne
import { UseCount, UseCountActions } from "./CounterProvider";
const CounterOne = () => {
const count = UseCount();
const dispatch = UseCountActions();
return (
<div>
<h2>count is: {count}</h2>
<button onClick={() => dispatch({ type: "add", value: 1 })}>
增加1
</button>
<button onClick={() => dispatch({ type: "decrement", value: 1 })}>
减少
</button>
<button onClick={() => dispatch({ type: "reset" })}>重置</button>
</div>
);
};
export default CounterOne;
CounterProvider
import React, { useReducer } from "react";
import { useContext } from "react";
const CounterContext = React.createContext();
const CounterContextDispatcher = React.createContext();
const initialState = 0;
const reducer = (state, action) => {
switch (action.type) {
case "add":
return state + action.value;
case "decrement":
return state - action.value;
case "reset":
return initialState;
default:
return state;
}
};
const CounterProvider = ({ children }) => {
const [count, dispatch] = useReducer(reducer, initialState);
return (
<CounterContext.Provider value={count}>
<CounterContextDispatcher.Provider value={dispatch}>
{children}
</CounterContextDispatcher.Provider>
</CounterContext.Provider>
);
};
export default CounterProvider;
export const UseCount = () => useContext(CounterContext);
export const UseCountActions = () => {
return CounterContextDispatcher;
};
请注意,我已经将代码中的HTML标签和一些英文内容进行了翻译。如果您有任何其他问题或需要进一步的帮助,请随时提问。
英文:
I want to write context with UseReducer hook but an error
error this:dispatch is not a function
what is problem?
please help me guys
almost is correct but not working it.
I want to see the appropriate action by clicking on the buttons,
one is increment, one is decrement and the other is reset.
CounterOne
import { UseCount, UseCountActions } from "./CounterProvider";
const CounterOne = () => {
const count = UseCount();
const dispatch = UseCountActions();
return (
<div>
<h2>count is:{count}</h2>
<button onClick={() => dispatch({ type: "add", value: 1 })}>
Addone{" "}
</button>
<button onClick={() => dispatch({ type: "decrement", value: 1 })}>
decrement
</button>
<button onClick={() => dispatch({ type: "reset" })}>reset</button>
</div>
);
};
export default CounterOne;
CounterProvider
import React, { useReducer, useState } from "react";
import { useContext } from "react";
const CounterContext = React.createContext();
const CounterContextDispather = React.createContext();
const initialState = 0;
const reducer = (state, action) => {
switch (action.type) {
case "add":
return state + action.value;
case "decrement":
return state - action.value;
case "reset":
return initialState;
default:
return state;
}
};
const CounterProvider = ({ children }) => {
const [count, dispatch] = useReducer(reducer, initialState);
return (
<CounterContext.Provider value={count}>
<CounterContextDispather.Provider value={dispatch}>
{children}
</CounterContextDispather.Provider>
</CounterContext.Provider>
);
};
export default CounterProvider;
export const UseCount = () => useContext(CounterContext);
export const UseCountActions = () => {
return CounterContextDispather;
};
答案1
得分: 0
导出 const UseCountActions = () => {
返回 useContext(CounterContextDispather);
};
这里有一个官方示例
<details>
<summary>英文:</summary>
export const UseCountActions = () => {
return useContext(CounterContextDispather);
};
There is a official [example](https://beta.reactjs.org/learn/scaling-up-with-reducer-and-context)
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论