Search code examples
reactjstypescriptreact-hooksreact-typescripttypescript1.8

Creating a React component with Typescript


I'm learning typescript lately, and I have to convert a fake react component to typescript, using the good practice.

For the moment I have something like that

import axios from "axios";
import { useEffect, useState } from "react";



//var component = (props: any) => {
export default const component = (props: any) => {

  //var listeOuNon = 0;
  let listeOuNon = 0;
  //var listes = props.lists;
  const = props.lists;
  listeOuNon = props.isLists;
  //var montrecomposant = props.show;
  const montreComposant = props.show;

  if (listeOuNon) {
    //var [trad, ajoutTrad] = useState();
    const [trad, ajoutTrad] = useState();

    useEffect(() => {
      (async () => {
        axios.get("/api/trad").then((reponse) => {
          if (reponse.status == 200) {
            ajoutTrad(reponse.data);
          }
        });
      })();
    });
  }

  //if (montrecomposant) {
  if (montreComposant) {
    return (
      <div>
        {listeOuNon ? (
          <div>
            <div>le super titre {trad}</div>
            <div>
              <ul>
                {listes.map((elements: any) => (
                  <li>{elements.text}</li>
                ))}
              </ul>
            </div>
          </div>
        ) : (
          <div>
            <div>le super titre</div>
            <div>Bonjour tout le monde</div>
          </div>
        )}
      </div>
    );
  } else {
    return <></>;
  }
};

//export default component;

I know that I should use the Typescript interface system, but I don't really know how to implement it. If there is a typescript expert, I would love some advices...


Solution

  • You could start of with replacing the Props. The useEffect and useState calls cannot be placed inside if statements so have to be moved.

    import {FC} from "react";
    
    interface IProps {
        isLists: boolean;
        lists: { text: '' }[];
        show: boolean;
    }
    
    const MyComponent: FC<IProps> = ({isLists, lists, show}) => {
        let listeOuNon = 0;
        const [trad, ajoutTrad] = useState('');
    
        useEffect(() => {
            if (isLists) {
    
                (async () => {
    
                    axios.get("/api/trad").then((reponse) => {
                        if (reponse.status == 200) {
                            ajoutTrad(reponse.data);
                        }
                    });
                })();
            }
        }, [isLists]);
    
        if (!show) {
            return null;
        }
    
        return (
            <div>
                {isLists ? (
                    <div>
                        <div>le super titre {trad}</div>
                        <div>
                            <ul>
                                {listes.map((elements: any) => (
                                    <li>{elements.text}</li>
                                ))}
                            </ul>
                        </div>
                    </div>
                ) : (
                    <div>
                        <div>le super titre</div>
                        <div>Bonjour tout le monde</div>
                    </div>
                )}
            </div>
        );
    };
    
    export default MyComponent;