Skip to content

Instantly share code, notes, and snippets.

@shahab65
Last active October 4, 2020 13:46
Show Gist options
  • Save shahab65/c7ce9137efd592389e91c19f7cf9c50f to your computer and use it in GitHub Desktop.
Save shahab65/c7ce9137efd592389e91c19f7cf9c50f to your computer and use it in GitHub Desktop.

ref

const divRef = React.useRef<HTMLDivElement>(null);
const myRef = React.useRef<HTMLElement | null>(null)

call back

interface Events {
  on(eventName: string, callback: () => void):void;
  trigger(eventName: string):void;
}

function with generic type as argument

interface ModelAttributes<T> {
  set(value: T): void;
 }

telling ts that our generic type will have some attributes:

interface HasId {
 id?: number;
}

export class Model<T extends HasId>{ 
}

using React.FC

import React from 'react';

interface MobileNumberInputProps {
  text: string;
  type: ButtonTypes;
  options: number[];
  action: ()=> void;
  }
  
  const MobileNumberInput : React.FC<MobileNumberInputProps> = ({text, type, action}) => {
    return (
        <div>hi</div>
    )
}
export default MobileNumberInput;

fa or en

import React from 'react';

type Props = {
  lang: "en" | "fa";
};

const App2 = (props: Props) => {
    const { lang} = props;
    if (lang === "en") {
        return (<div>welcome</div>)
    }
    return (
        <div>
            خوش امدید
        </div>
    );
};

export default App2;

boolean

const testFunc = (value:boolean):void => {console.log(value)}

types on useState

 const [counter, setCounter] = React.useState<number>(0)

You can also use a union type like this <number | null> if you don't have an initial state.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment