美文网首页
React hooks

React hooks

作者: 柒轩轩轩轩 | 来源:发表于2021-05-11 02:38 被阅读0次

React component class 的缺点

  1. 大型组件很难拆分和重构,也很难测试。
  2. 业务逻辑分散在组件的各个方法之中,导致重复逻辑或关联逻辑。
  3. 组件类引入了复杂的编程模式,比如 render props 和高阶组件。

React Hooks 的设计目的,就是加强版函数组件,完全不使用"类",就能写出一个全功能的组件。

Hooks are a way to write reusable code, instead of more classic techniques like inheritance

  1. useState ==> Function that lets you use state in a functional component
  2. useEffect ==> Function that lets you use something like lifecyle methods in a functional component
  3. useRef ==> Function that lets you create a 'ref' in a function component
const [activeIndex, setActiveIndex]=useState(null)

activeIndex --> piece of state
setActiveIndex --> function to change piece of state
null --> initial value for piece of state

useEffect

. Allows function components to use something like lifecycle methods

. We configure the hook to run some code automatically in one of three scenarios

  1. When the component is rendered for the first time only
  2. when the component is rendered for the first time and whenever it rerenders
  3. when the component is rendered for the first time and (whenever it rerenders and some piece of data has changed)
useEffect(()=>{},)

after,
[] ==> run at initial render
nothing ==> run at initial render and run after every rerender
[data]==> run at initial render and run after every rerender if data has changed since last render

useEffect(()=>{
console.log('1');
    return () =>{
      console.log('2');
    }
  },[term]
)

whenever our component first renders, the overall arrow function is invoked and we return the arrow function 2
then anything when run the whole function again, first react is going to call the 2 functions that it got from the last time use effect ran, then it gonna call the overall function again

当在dropdown component中,想要实现点击component外部从而实现dropdown的回缩
使用manually click listener

相关文章

  • React Hooks

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

  • react-hooks

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

  • React Hooks

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

  • 5分钟简单了解React-Hooks

    首先附上官网正文?:React Hooks Hooks are a new addition in React 1...

  • react-hooks

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

  • React-hooks API介绍

    react-hooks HOOKS hooks概念在React Conf 2018被提出来,并将在未来的版本中被...

  • React Hooks 入门

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

  • react hooks 源码分析 --- useState

    1. react hooks简介 react hooks 是react 16.8.0 的新增特性,它可以让你在不编...

  • React Hooks的入门简介

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

  • React hooks(钩子)

    React hooks(钩子) React hooks 是React 16.8中的新增功能。它们使您无需编写类即可...

网友评论

      本文标题:React hooks

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