函数组件没有自己的state和生命周期,又叫无状态组件,没有自己的state,所以也可以将之称之为展示型组件

如何使用useState:

usestate是一个hook,该hook允许我们在函数组件中使用state

必须在函数组件内部使用

  1. 通过usestate在函数组件中写了一个state
  2. count表示state
  3. setCount修改state的方法
  1. import React,{FC,useState} from "react"
  2. // Fc是函数组件的类型
  3. type IPorps={
  4. name:string,
  5. age:number
  6. }
  7. const Say:FC<IPorps> = (props) => {
  8. //通过usestate在函数组件中写了一个state
  9. //count表示state
  10. //setCount修改state的方法
  11. let [count,setCount]=useState<number>(0)
  12. const clickHandler=()=>{
  13. setCount(++count);
  14. }
  15. return <div>
  16. 这是一个函数组件{props.name}
  17. 当前的count是:{count}
  18. <button onClick={clickHandler}>点我修改count</button>
  19. </div>
  20. }
  21. export default Say

修改state之后无法拿到最新的状态,要等到下一个事件循环周期执行时,状态才是最新的

  1. import { stringify } from "querystring"
  2. import React,{FC,useState} from "react"
  3. // Fc是函数组件的类型
  4. type IPorps={
  5. name:string,
  6. age:number
  7. }
  8. const Say:FC<IPorps> = (props) => {
  9. //通过usestate在函数组件中写了一个state
  10. //count表示state
  11. //setCount修改state的方法
  12. const [people,setPeople]=useState<{name:string;age:number}>({
  13. name:"wangsu",
  14. age:23
  15. })
  16. const clickHandler1=()=>{
  17. setPeople({
  18. name:"yeye",
  19. age:18
  20. })
  21. console.log(people.name)
  22. }
  23. return <div>
  24. 这个的年龄是:{people.age},姓名是:{people.name}
  25. <button onClick={clickHandler1}>点我修改姓名</button>
  26. </div>
  27. }
  28. export default Say

image.png
解决办法是同步在写一遍

  1. const clickHandler1=()=>{
  2. setPeople({
  3. name:"yeye",
  4. age:18
  5. })
  6. //在写一遍,设置name
  7. people.name="yeye";
  8. }

注意点一:

usestate的数据不是合并,而是用一个新的数组去替换掉原来的数据

  1. import { stringify } from "querystring"
  2. import React,{FC,useState} from "react"
  3. // Fc是函数组件的类型
  4. type IPorps={
  5. name:string,
  6. age:number
  7. }
  8. const Say:FC<IPorps> = (props) => {
  9. const [people,setPeople]=useState<{name?:string;age?:number;gender?:string}>({
  10. name:"wangsu",
  11. age:23
  12. })
  13. const clickHandler=()=>{
  14. setCount(++count);
  15. }
  16. const clickHandler1=()=>{
  17. setPeople({
  18. gender:"男"
  19. })
  20. }
  21. return (<div>
  22. {people.gender}
  23. {console.log(people)}//这里的结果是:{gender: "男"}
  24. <button onClick={clickHandler1}>点我修改姓名</button>
  25. </div>)
  26. }
  27. export default Say

注意点二:

useState中的数据务必是immutable数据
不能在原来基础上进行修改,要完全换一个对象,内存地址发生改变才能触发页面进行刷新

  1. import React, { FC, useState } from "react";
  2. // Fc是函数组件的类型
  3. type IPorps = {
  4. name: string;
  5. age: number;
  6. };
  7. const Say: FC<IPorps> = (props) => {
  8. //通过usestate在函数组件中写了一个state
  9. //count表示state
  10. //setCount修改state的方法
  11. let [count, setCount] = useState<number>(0);
  12. const [people, setPeople] = useState<{
  13. name?: string;
  14. age?: number;
  15. gender?: string;
  16. }>({
  17. name: "wangsu",
  18. age: 23,
  19. });
  20. const [list, setList] = useState<number[]>([
  21. 1,
  22. 3,
  23. 5,
  24. 2,
  25. 13,
  26. 9,
  27. 45,
  28. 30,
  29. 2,
  30. 50,
  31. 4,
  32. ]);
  33. const clickHandler = () => {
  34. setCount(++count);
  35. };
  36. const clickHandler1 = () => {
  37. setPeople({
  38. gender: "男",
  39. });
  40. };
  41. const clickHandler2 = () => {
  42. list.sort((a, b) => {
  43. return a - b;
  44. });
  45. //这里触发了内存地址发生改变
  46. setList([...list])
  47. };
  48. return (
  49. <div>
  50. {people.gender}
  51. {list}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  52. {console.log(people)}
  53. <button onClick={clickHandler}>点我修改count</button>
  54. <button onClick={clickHandler1}>点我修改姓名</button>
  55. <button onClick={clickHandler2}>点我进行排序</button>
  56. </div>
  57. );
  58. };
  59. export default Say;