这是一个可怕的问题,在 React 中,我们经常会编写条件语句来显示不同的视图,比如这个简单的例子。

const App = () => {

  return (

    <>

      {

        loading ?  : 

      }

    

  )

}

但是,当项目的代码量足够大并且有很多 JSX 条件时,事情很快就会失控。代码变得非常混乱且可读性差。像下面这段代码,我真的没有勇气和信心去了解它的细节。

import React, { useState } from "react"

export default function ConditionDemo() {

  const [ loading ] = useState(false)

  const [ isLogin ] = useState(true)

  const [ hasAuth ] = useState(false)

  return (

    <>

      {

        loading ?

          

            loading