Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type 'boolean' is not assignable to type 'ReactElement<any, any>'

Please can someone explain how to fix error that TS throws: Type 'boolean' is not assignable to type 'ReactElement<any, any>'. In my code i want to show <Confetti/> component depending on isCorrect value. If var has falsy value i need nothing to render. How to make this type of logic correctly?

import React, { FC } from 'react'
import ReactConfetti from 'react-confetti';
import { useWindowSize } from 'react-use';

interface ConfettiProps {
    isCorrect: boolean;
}

interface WindowDimensions {
    width: number;
    height: number;
  }

const Confetti: FC<ConfettiProps> = ({isCorrect}) => {
    const { width, height }: WindowDimensions = useWindowSize();

  return (
    isCorrect && (<ReactConfetti width={width} height={height}/> )
  )
}

export default Confetti; 
like image 458
dinocode Avatar asked Aug 23 '26 03:08

dinocode


2 Answers

Two options:

return (
  isCorrect ? (<ReactConfetti width={width} height={height}/> ) : null
)
return (
  <>{isCorrect && (<ReactConfetti width={width} height={height}/> )}</>
)

<></> is a react framgment

like image 138
Konrad Linkowski Avatar answered Aug 25 '26 22:08

Konrad Linkowski


I had this error because I was using a .ts, not a .tsx file. So if you're using a ts file, change the extension to .tsx. I hope this helps!

like image 36
Jeyvers Avatar answered Aug 25 '26 20:08

Jeyvers



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!