React的状态管理useState

基础使用

useState 是一个 React Hook(函数),它允许我们向组件添加一个状态变量, 从而控制影响组件的渲染结果和普通JS变量不同的是,状态变量一旦发生变化组件的视图UI也会跟着变化(数据驱动视图)
useState类似一个java的实体类,实体类通过set方法来改变对象属性值

function App(){
//count是变量,setCount是设置变量的值,React.useState(0)设置count为0
  const [ count, setCount ] = React.useState(0)
  return (
    <div>
      <button onClick={()=>setCount(count+1)}>{ count }</button>
    </div>
  )
}

状态的修改规则

在React中状态被认为是只读的,我们应该始终替换它而不是修改它, 直接修改状态不能引发视图更新
在这里插入图片描述

修改对象状态

对于对象类型的状态变量,应该始终给set方法一个全新的对象 来进行修改,不能修改对象的属性
在这里插入图片描述

组件的基础样式处理

React组件基础的样式控制有俩种方式,行内样式和class类名控制

<div style={{ color:'red'}}>this is div</div>
.foo{
  color: red;
}
import './index.css'

function App(){
  return (
    <div>
      <span className="foo">this is span</span>
    </div>
  )
}

相关推荐

  1. react状态管理useState

    2024-04-30 22:20:01       20 阅读
  2. React Hooks、useState、useEffect 、react函数状态

    2024-04-30 22:20:01       46 阅读
  3. ReactuseState 原理

    2024-04-30 22:20:01       31 阅读
  4. React HooksuseState、useRef使用

    2024-04-30 22:20:01       62 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-30 22:20:01       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-30 22:20:01       106 阅读
  3. 在Django里面运行非项目文件

    2024-04-30 22:20:01       87 阅读
  4. Python语言-面向对象

    2024-04-30 22:20:01       96 阅读

热门阅读

  1. 商城数据库88张表

    2024-04-30 22:20:01       28 阅读
  2. linux驱动-CCF-1 provider 注册时钟

    2024-04-30 22:20:01       21 阅读
  3. 自制英语听力视频 4.30

    2024-04-30 22:20:01       28 阅读
  4. UI设计饱和了吗?赚钱吗?

    2024-04-30 22:20:01       34 阅读
  5. Method

    Method

    2024-04-30 22:20:01      28 阅读
  6. python字节串和数字互转

    2024-04-30 22:20:01       31 阅读
  7. 程序包 org.junit 不存在解决方案

    2024-04-30 22:20:01       31 阅读
  8. 安全架构概述

    2024-04-30 22:20:01       26 阅读
  9. 【工程记录】ChatGLM3-6B微调实践的更新说明

    2024-04-30 22:20:01       34 阅读