react18更新了什么

升级

npm i react@18 react-dom@18

原来

jsx 复制代码
import {render} from 'react-dom';
const rootElement = document.getElementById('root')
render(<App/>,rootElement)

现在

jsx 复制代码
import {createRoot} from 'react-dom/client';
const rootElement = document.getElementById('root')
const root=createRoot(rootElement)
root.render(<App/>,rootElement)

18的升级是渐进式升级,很多新功能是可选的,并不会因为升级对以前的代码带来破坏性的影响

放弃兼容ie

17能兼容ie11,18不兼容,如果要兼容,只能回退react版本。

automatic Batching自动批处理

jsx 复制代码
const [count, setCount]=useState(0);
const [name, setName]=useState('');
setTimeout(()=>{
  setCount(1);
  setName('lwp');
})

18之前:异步里面不会自动批处理,会触发两次set 18之后会触发一次set 如果不想使用automatic Batching

jsx 复制代码
import {flushSync} from 'react-dom'
flushSync(()=>{
  setCount(1);
  setCount(2);
})

不想用automatic batching场景

  • 你需要在状态之后,立刻读取dom上的数据

在17之前,class组件的异步代码里面的setState是同步,而18之后所有的场景下都是异步的,如果想要变成同步,依然用flushSync

jsx 复制代码
class Counter{
  constructor(){
    this.state={
      count:0
    }
  }
  fn=()=>{
    setTimeout(()=>{
      this.setState({count:count+1})
      //React 17:1
      //React 18:0
      console.log(count)
  })
  }
}
jsx 复制代码
  setTimeout(()=>{
    flushSync(()=>{
        this.setState({count:count+1})
        //React 17:1
        //React 18:0
        console.log(count)
    })
  })

返回值

原来组件只能返回null,现在既可以返回null也可以返回undefined

concurrent并发渲染

原来渲染只能一个一个触发(串行),且不可中断 现在

  • 渲染可暂停、可继续、可终止
  • 渲染可以在后台进行
  • 渲染可以有优先级

transitions

渲染优先级里面分为高优先级和低优先级,默认高优先级,通过transitions来标记低优先级 class

jsx 复制代码
import {startTransitions} from 'react'
startTransitions(()=>{
  xxx
})

hook

jsx 复制代码
import {useTransitions} from 'react'
const [pending, startTransitions] = useTransitions()
startTransitions(()=>{
  xxx
})

Suspense

以前我们进行异步请求都是fetch then render

jsx 复制代码
function Father(){
  const [title,setTitle]=useState();
  useEffect(()=>{
    getTitle().then(res=>{
      setTitle(res)
    })
  },[])
  return (<div>
    {!title?<Loading/>:title}
    <Child/>
  </div>)
}
function Child(){
    const [list,setList]=useState();
  useEffect(()=>{
    getList().then(res=>{
      setList(res)
    })
  },[])
  return (<div>
    {!list&&<Loading/>}
    {list&&list.map(item=>(<div>{item}</div>))}
  </div>)
}

这样就会导致两个组件的请求是串行,增加渲染时间 为了改变这种方式,可以通过Promise.all改造成并行,但是这种方式依然有缺点,就是两次请求必须都完成才能进行渲染。于是有了Suspense。fetch as you render

jsx 复制代码
function Father(){
  const [title,setTitle]=useState();
  useEffect(()=>{
    getTitle().then(res=>{
      setTitle(res)
    })
  },[])
  return (<Suspense fallback=(<Loading/>)>
    
    <Suspense fallback=(<Loading/>)><Child/></Suspense>
  </Suspense>)
}
function Child(){
    const [list,setList]=useState();
  useEffect(()=>{
    getList().then(res=>{
      setList(res)
    })
  },[])
  return (<div>
    {list.map(item=>(<div>{item}</div>))}
  </div>)
}
jsx 复制代码
//并行
<Suspense1></Suspense1>
<Suspense2></Suspense2>
//串行
<Suspense1><Suspense2></Suspense2></Suspense1>

参考

相关推荐
xiaofeichaichai3 小时前
Webpack
前端·webpack·node.js
问心无愧05133 小时前
ctf show web入门111
android·前端·笔记
唐某人丶3 小时前
模型越来越强,我们还需要 Agent 工程吗?—— 从价值重估到 Harness 实践
前端·agent·ai编程
智码看视界3 小时前
现代Web开发基础:全栈工程师的起航点
前端·后端·c5全栈
JS菌4 小时前
手写一个 AI Agent 全栈项目:从沙箱执行到子智能体的完整实现
前端·人工智能·后端
excel5 小时前
HLS TS 文件损坏的元凶:Git 提交与拉取
前端
Aphasia3115 小时前
https连接传输流程
前端·面试
徐小夕5 小时前
万字长文!千万级文档 RAG 知识库系统落地实践
前端·算法·github
threelab5 小时前
Three.js 物理模拟着色器 | 三维可视化 / AI 提示词
开发语言·前端·javascript·人工智能·3d·着色器