• 定义变量 useState
    1. import React, {useState} from 'react';
    2. function App() {
    3. /**
    4. * count: 状态
    5. * setCount 修改状态的方法
    6. */
    7. const [count, setCount] = useState(10)
    8. return (
    9. <div className="App">
    10. fbd --- {count}
    11. <button onClick={() => {setCount(count+1)}}>增加</button>
    12. </div>
    13. );
    14. }
    15. export default App;
    • React.memo

    只有改变了才渲染

    1. import React from 'react';
    2. const Child = ({count}) =>{
    3. console.log('render')
    4. return <p>{count}</p>
    5. }
    6. export default React.memo(Child)
    • 依赖关系 useCallback
    1. import React, {useState, useCallback} from 'react';
    2. const Demo1 = () =>{
    3. const [age, setAge] = useState(1)
    4. const [age1, setAge1] = useState(1)
    5. function handle() {
    6. setAge(age+1)
    7. }
    8. // useCallback 第二个参数 决定第一个函数方法是否可以执行,, 但是第一次会执行一次
    9. // 如果 数组中的参数 发生变化 则可以执行
    10. // useCallback 有依赖关系,是否执行
    11. return (
    12. <div>
    13. <p>{age} ll</p>
    14. <button onClick={handle}>增加</button>
    15. <hr/>
    16. <p>{age1} ll</p>
    17. <button onClick={useCallback(() => {setAge1(age1+1)}, [age])}>增加</button>
    18. </div>
    19. )
    20. }
    21. export default Demo1
    • useReducer
    1. import React, {useState, useReducer} from 'react';
    2. const initialState = { count: 0 }
    3. function reducer(state, action) {
    4. switch(action.type) {
    5. case 'increment':
    6. return {
    7. count: state.count+1
    8. }
    9. case 'decrement':
    10. return {
    11. count: state.count-1
    12. }
    13. default:
    14. throw new Error('错误')
    15. }
    16. }
    17. function Counter() {
    18. // const [count, setCount] = useState(0)
    19. const [state, dispatch] = useReducer(reducer, initialState)
    20. return (
    21. <div>
    22. count: {state.count}
    23. <button onClick={() => dispatch({type: 'increment'})}>+++</button>
    24. <button onClick={() => dispatch({type: 'decrement'})}>---</button>
    25. </div>
    26. )
    27. }
    28. export default Counter
    • useEffect 生命周期 , 可以写多个
    1. import React, {useState, useEffect} from 'react';
    2. export default () => {
    3. const [count, setCount] = useState(0)
    4. /**
    5. * 相当于 三个生命周期 didmount didupdate
    6. * WillUnMount
    7. *
    8. * 第二个参数 是[] 相当于 didMount
    9. * [count] count改变 才会触发
    10. * 函数内 return 函数 相当于 unmount
    11. */
    12. useEffect(() => {
    13. console.log(count)
    14. return () => {
    15. // 组件卸载
    16. }
    17. }, [])
    18. const handle = () => {
    19. let random = Math.random()
    20. setCount(random)
    21. }
    22. return (
    23. <div>
    24. <p>{count}</p>
    25. <button onClick={handle}>点击</button>
    26. </div>
    27. )
    28. }

    • Todo
    1. //////list.jsx
    2. import React from 'react';
    3. import Form from './Todo'
    4. import { useState } from 'react';
    5. const TodoList = () => {
    6. const [todos, setTodos] = useState([])
    7. function setValue(text) {
    8. setTodos([text, ...todos])
    9. }
    10. function clear() {
    11. setTodos([])
    12. }
    13. return (
    14. <div>
    15. <Form onSubmit={setValue}/>
    16. <div>
    17. {
    18. todos.map((el,i) => {
    19. console.log(el)
    20. return (
    21. <div key={i}>
    22. {el}
    23. </div>
    24. )
    25. })
    26. }
    27. </div>
    28. <button onClick={clear}>clear</button>
    29. </div>
    30. )
    31. }
    32. export default TodoList
    • form.jsx ```javascript // export default TodoForm import React, {useState} from ‘react’ // 提炼出来 const useInputValue = (initialValue) => { const [value, setValue] = useState(initialValue)

      return { value, onChange: e=>setValue(e.target.value), resetValue: () => setValue(‘’) } }

    const TodoForm = ({onSubmit}) => { text是剩余的对象集合 const {resetValue, …text} = useInputValue(‘’)

    const onSub =(e) => { e.preventDefault() onSubmit(text.value) //触发父组件的方法 resetValue() } return (

    ) }

    export default TodoForm

    1. ---
    2. - useRef() ,子组件在重渲染时候会重新赋值初始值,每次改变不了。使用useRef会记住每次的值
    3. 获取dom focus
    4. ```javascript
    5. let node = useRef(null)
    6. <input type="text"
    7. value={value}
    8. className="form-control col-8"
    9. ref={node}
    10. onChange={ e => setValue(e.target.value)}
    11. />
    12. useEffect(() => {
    13. if (inputActive) {
    14. node.current.focus()
    15. }
    16. }, [inputActive])