Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Typescript: How to fix this error -> "element is not assignable to string"

I’m trying to create a popup banner with the following code (sorry if the format is off)

 //this is in folder Banner.tsx

 import React, {useCallback} from "react";
 type Properties = {
     close: () => void;
     text: string;

 const Banner: React.FC<Properties> = ({close, text}) => {
       const onClick = useCallback(() => {
                       close();},
                       [close, text]);
       return (
          <div>
              <span className = "popup"> {onClick}{close}[x]
              </span>
              {text}
         </div>
         );
         };
       export default Banner;

//this is in App.tsx
 import Banner from "./Components/Banner";
 function App(): JSX.Element {

 const [isOpen, setIsOpen]=useState(false);
      const toggleBanner = () => {
         SetIsOpen(!isOpen);
};

 return (
     <div>
        <input type = "button"
              value = "popup"
              onClick={toggleBanner}/>
        <p>hi</p>
        {isOpen && <Banner text = {<><b>testing</b><p>testing popup</p><button>testing button</button></>} handleClose={toggleBanner}/>}
      </div>
export default App;

but i keep getting this error and i’m not sure how to fix it -> type element is not assignable to type "string". the error is on this line: {isOpen && <Banner text = {<>testing

testing popup

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

testing button</>} handleClose={toggleBanner}/>}

>Solution :

That’s because in

<Banner text = {<>testing testing popup testing button</>} handleClose={toggleBanner}/>}

text attribute expects a string not an element. You have defined it yourself here

 type Properties = {
     close: () => void;
     text: string; //this line
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading