<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      [React] Use the useReducer Hook and Dispatch Actions to Update State (useReducer, useMemo, useEffect)

      As an alternate to useState, you could also use the useReducer hook that provides state and a dispatch method for triggering actions. In this lesson, we’ll centralize logic that is spread across a web application and centralize it using the useReducer hook.

      NOTE: Since hooks are still a proposal and only pre-released, it’s not currently recommended to use them in production.

      useReducer:

        const [todos, dispatch] = useReducer((state, action) => {
          switch (action.type) {
            case "ADD_TODO":
              todoId.current += 1;
              return [
                ...state,
                { id: todoId.current, text: action.text, completed: false }
              ];
            case "DELETE_TODO":
              return state.filter(todo => todo.id !== action.id);
            case "TOGGLE_TODO":
              return state.map(todo =>
                todo.id === action.id ? { ...todo, completed: !todo.completed } : todo
              );
            default:
              return state;
          }
        },initialValue);

       

      useReducer can accept second param as a function, so we can replace 'initialValue' with a function return a 'initialValue', which means we can using cache for the function if the param doesn't change, we always return the cache. To do that we can use 

       

      useMemo:

        const [todos, dispatch] = useReducer((state, action) => {
          switch (action.type) {
            case "ADD_TODO":
              todoId.current += 1;
              return [
                ...state,
                { id: todoId.current, text: action.text, completed: false }
              ];
            case "DELETE_TODO":
              return state.filter(todo => todo.id !== action.id);
            case "TOGGLE_TODO":
              return state.map(todo =>
                todo.id === action.id ? { ...todo, completed: !todo.completed } : todo
              );
            default:
              return state;
          }
        }, useMemo(initialValue, []));

      The second parameter of 'useMemo' indicates when the memorized version should change. In our case, we want it to always be the same, so passing an empty array conveys that message.

       

      To handle side effect of action, in our case, is update localstorage, we can use useEffect:

      useEffect(
        () => {
          window.localStorage.setItem("todos", JSON.stringify(todos));
        },
        [todos]
      );

       

      ---

       

      posted @ 2019-02-24 17:28  Zhentiw  閱讀(443)  評論(0)    收藏  舉報
      主站蜘蛛池模板: 南通市| 春色校园综合人妻av| 麻豆国产黄色一级免费片| 国产精品久久久一区二区| 久色伊人激情文学你懂的| 亚洲综合精品第一页| 玖玖在线精品免费视频| 国产免费高清69式视频在线观看| 亚洲综合伊人久久大杳蕉| 久久综合亚洲鲁鲁九月天| 亚洲av色在线播放一区| 男女18禁啪啪无遮挡激烈网站| 欧美人与动牲交A免费观看| 中文字幕无码不卡一区二区三区| 国产成人综合久久亚洲精品| 国产精品无码a∨麻豆| 中文 在线 日韩 亚洲 欧美| 国产熟睡乱子伦视频在线播放| 亚洲av首页在线| 黄色一级片一区二区三区| 亚洲欧美中文字幕日韩一区二区| 884aa四虎影成人精品| 成人乱码一区二区三区四区| 昂仁县| 日韩成人午夜精品久久高潮| 亚洲色拍拍噜噜噜最新网站| 婷婷四房播播| 在线综合亚洲欧洲综合网站| 国产精品视频中文字幕| 精品欧洲av无码一区二区| 久久国产精品精品国产色| 狠狠亚洲色一日本高清色| 国产精品久久蜜臀av| 国产玖玖视频| 欧美乱妇狂野欧美在线视频| 国产精品免费AⅤ片在线观看| 久久精品激情亚洲一二区| 狠狠色丁香婷婷综合尤物| 国产69精品久久久久乱码免费 | 成人无码视频| 四虎成人精品永久网站|