美文网首页
Deep Dive React 3 React Hooks

Deep Dive React 3 React Hooks

作者: 吴摩西 | 来源:发表于2022-08-16 09:47 被阅读0次

React Hooks allow us to use React features without writing a class

  • state useState, useReducer
  • component lifecycle useEffect

A lot of questions surrounding react hooks

  • why can't we call hooks inside loops or conditions? (why do hooks rely on call order?)
const ReactX = (() => {
  let hooks = [];
  let index = 0;
  const useState = (initialValue) => {
    const localIndex = index;
    index++;
    if (hooks[localIndex] === undefined) {
      hooks[localIndex] = initialValue;
    }
    const setterFunction = (newValue) => (hooks[localIndex] = newValue);
    return [hooks[localIndex], setterFunction];
  };
  const resetIndex = () => {
    index = 0;
  };

  const useEffect = (callback, dependencyArray) => {
    let hasChanged = true;
    const oldDependencies = hooks[index];
    if (oldDependencies) {
      hasChanged = false;
      dependencyArray.forEach((dependency, index) => {
        const oldDependency = oldDependencies[index];
        const areTheSame = Object.is(dependency, oldDependency);
        if (!areTheSame) {
          hasChanged = true;
        }
      });
    }
    if (hasChanged) {
      callback();
    }
    hooks[index] = dependencyArray;
    index ++;
  };

  return {
    useState,
    useEffect,
    resetIndex
  };
})();

const { useState, useEffect, resetIndex } = ReactX;
const Component = () => {
  const [counterValue, setCounterValue] = useState(1);
  console.log(counterValue);
  useEffect(() => {
    console.log('useEffect');
  }, []);
  if (counterValue !== 2) {
    setCounterValue(2);
  }
};
Component();
resetIndex();
Component();
  • Why isn't X A HOOK?
    • useProvider()
    • useCatch()
    • useBailout()

Composition = hooks don't conflict with each other
Debugging = bugs should be easy to find

const Button = ({ color }) => {
  // it will bail out re-render unless callback returns true
  useBailout(previousColor => previousColor !== color, color);
  return (<button className={`${color}-button`}>Button</button>)
}
  • useMemo() does similar work to useBailout()
    • it doesn't explicitly bail out of re-rendering

How can useState() be a part of rendering?

  • rendering is handled by React DOM, but we aren't importing useState() from React DOM
  • when useState() gets called, the call gets forwarded to dispatcher
// inside React
const React = {
  // ...
  __currentDispatcher: null,
  useState(initialState) {
    return React.__currentDispatcher.useState(initialState);
  }
};
// inside React DOM
const previousDispatcher = React.__currentDispatcher;
React.__currentDispatcher = ReactDOMDispatcher;
let result;
try {
  result = Component(props);
} finally {
  React.__currentDispatcher = previousDispatcher;
}

相关文章

  • Deep Dive React 3 React Hooks

    React Hooks allow us to use React features without writin...

  • React Hooks - 深度解析 useContext 和

    作者 Austin Johnston, 翻译,侵删React Hooks - A deeper dive feat...

  • react-native hooks

    react-native v0.59.0-rc.3增加React Hooks特性 react hooks 是一个已...

  • React Hooks

    React Hooks Hooks其实就是有状态的函数式组件。 React Hooks让React的成本降低了很多...

  • React Hooks

    前言 React Conf 2018 上 React 提出了关于 React Hooks 的提案,Hooks 作为...

  • react-hooks

    react-hooks react-hooks 是react16.8以后,react新增的钩子API,目的是增加代...

  • react-hooks

    前置 学习面试视频 总结react hooks react-hooks react-hooks为函数组件提供了一些...

  • React Hooks的入门简介

    什么是React Hooks? 首先React Hooks是React生态圈里的新特性,它改变了传统react的开...

  • React学习系列之 React Hooks

    React Hooks React 16.8、useState、 React 16.8.0 is the firs...

  • React Hooks 入门

    React Hooks 是 React v16.8 版本引入了全新的 API。 React Hooks 基本概念 ...

网友评论

      本文标题:Deep Dive React 3 React Hooks

      本文链接:https://www.haomeiwen.com/subject/tipkgrtx.html