首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >简单的学习useReducer

简单的学习useReducer
EN

Stack Overflow用户
提问于 2021-02-18 10:05:02
回答 3查看 259关注 0票数 0

我正在从官方网站学习useReducer,我的代码不能正常工作,我不确定initialCount应该如何工作。任何帮助都将不胜感激!

代码语言:javascript
复制
import React, { useReducer } from "react";

const reducer = (state, action) => {
  switch (action.type) {
    case "increment":
      return { count: state.count + 1 };
      break;
    case "decrement":
      return { count: state.count - 1 };
      break;
    default:
      throw new Error();
  }
};

const UseReduceExample = () => {
  const [state, dispatch] = useReducer(reducer, initialCount );

  return (
    <>
      Count: {state.count}
      <button onClick={() => dispatch({ type: "increment" })}>+</button>
    </>
  );
};

export default UseReduceExample;
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2021-02-18 10:10:28

您应该传递您的初始状态,请参见下面的片段。

代码语言:javascript
复制
 import React, { useReducer } from "react";


const initialState = {
  count: 0
};

const reducer = (state, action) => {
  switch (action.type) {
    case "increment":
      return { count: state.count + 1 };
      break;
    case "decrement":
      return { count: state.count - 1 };
      break;
    default:
      throw new Error();
  }
};

const UseReduceExample = () => {
  const [state, dispatch] = useReducer(reducer, initialState);

  return (
    <>
      Count: {state.count}
      <button onClick={() => dispatch({ type: "increment" })}>+</button>
      <button onClick={() => dispatch({ type: "decrement" })}>-</button>
    </>
  );
};

export default UseReduceExample;
票数 2
EN

Stack Overflow用户

发布于 2021-02-18 10:10:07

代码语言:javascript
复制
const initState = {
   count: 0
}
const reducer = (state=initState, action) => {
  switch (action.type) {
    case "increment":
      return { count: state.count + 1 };
    case "decrement":
      return { count: state.count - 1 };
    default:
      return state
  }
};
票数 1
EN

Stack Overflow用户

发布于 2021-02-18 10:25:40

这样做更好,创建一个单独的文件,并称它为您想要的ex MainStore。然后添加这样的状态逻辑:

代码语言:javascript
复制
import React, { createContext, useReducer, useEffect } from 'react';

export const MainContext = createContext({});

export const initialState = {
  increment:'',
  decrement:''

};

function reducer(state, action) {
  switch (action.type) {
     case "increment":
      return { count: state.count + 1 };
    case "decrement":
      return { count: state.count - 1 };
    default:
      throw new Error('Action type must be defined');
  }
}

const MainStore = ({ children }) => {
  const [state, dispatch] = useReducer(reducer, initialState);

  return (
    <MainContext.Provider value={{ state, dispatch }}>
      {children}
    </MainContext.Provider>
  );
};

export default MainStore;

然后像这样包装使用这种状态的每个组件。

代码语言:javascript
复制
    <MainStore>
      <UseReduceExample  />
      <AnotherComponent />
    </MainStore>

    And to use state and dispatch inside those components do it like this.

      import React, {useContext} from 'react'
      import { MainContext } from 'path/to/store/MainStore';
    
      const UseReduceExample = () => {
      const [state, dispatch] = useContext(MainStore);
    
      return (
        <>
          Count: {state.count}
          <button onClick={() => dispatch({ type: "increment" })}>+</button>
        </>
      );
    };

export default UseReduceExample;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66257666

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档