Ad

Is It Possible In React UseState(<>) Has Initial State Is Empty Div Or Component?

- 1 answer

my code is like that

const [component, setComponent ] = useState( ? )

What will be the initial value for component

if(true)
  setComponent(<ComponentA />)
else
  setComponent(<ComponentB />)
Ad

Answer

Personally I wouldn't store components in state but rather data to determine what is displayed. For example:

const [value, setValue] = useState(true)

return (
  value ? <ComponentA /> : <ComponentB />
)
Ad
source: stackoverflow.com
Ad