加入收藏 | 设为首页 | 会员中心 | 我要投稿 核心网 (https://www.hxwgxz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程 > 正文

effect的更新依赖屈指可数

发布时间:2021-05-25 01:45:30 所属栏目:编程 来源:互联网
导读:useEffect中的第二个参数,可以是一个参数数组(依赖数组)。React更新DOM的思想,不管过程怎样,只将结果展示给世人。 React在更新组件的时候,会对比props,通过

      setCount(x => x + 1);  // 传递参数为一个函数时候,默认传递的第一个参数是之前的值,这是useState的hook在处理 

    }, 1000); 

    return () => clearInterval(id); 

  }, []); 

 

  return <h1>{count}</h1>; 

 

// 使用useReducer 

function Counter({ step }) { 

  const [count, dispatch] = useReducer(reducer, 0); 

 

  function reducer(state, action) { 

    if (action.type === 'tick') { 

      return state + step; 

    } else { 

      throw new Error(); 

    } 

  } 

 

  useEffect(() => { 

    const id = setInterval(() => { 

      dispatch({ type: 'tick' }); 

    }, 1000); 

    return () => clearInterval(id); 

  }, [dispatch]); 

 

  return <h1>{count}</h1>; 

(编辑:核心网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读