Skip to content

Commit 4fbcd0e

Browse files
Typescript in React
1 parent dd77de2 commit 4fbcd0e

File tree

1 file changed

+119
-1
lines changed

1 file changed

+119
-1
lines changed

react.js

Lines changed: 119 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1908,4 +1908,122 @@ debugger;
19081908
expect(title).toBeDefined();
19091909
});
19101910
});
1911-
});
1911+
});
1912+
1913+
1914+
1915+
// TESTING VISUAL WITH STORYBOOK
1916+
// npx -p @storybook/cli sb init
1917+
// npm run storybook
1918+
// work with file.stories.js
1919+
1920+
1921+
1922+
1923+
// TYPESCRIPT IN REACT
1924+
1925+
// install easy with create-react-app
1926+
// create-react-app project-name --template typescript
1927+
1928+
// or later on with npm
1929+
// npm install typescript @types/react @types/react-dom @types/node
1930+
1931+
// some library need to have a special tsx install
1932+
// react-router-dom needs npm install @types/react-router-dom
1933+
1934+
// work in .tsx files; you can change files one at a time
1935+
// test Typescript live -> https://www.typescriptlang.org/play
1936+
// nice source -> https://react-typescript-cheatsheet.netlify.app/docs/basic/setup
1937+
1938+
const id: number = 1;
1939+
const title: string = 'Hello';
1940+
const hidden: boolean = false;
1941+
const arr: string[] = ['one', 'two', 'three'];
1942+
const arr: Array<string> = ['one', 'two', 'three'];
1943+
let code: (string | number); code = 123; code = "ABC"; // both work
1944+
const hidden: any = false;
1945+
const arr: any[] = ['one', 2, 'three'];
1946+
function sayHi(): void { console.log('Hi!') } // void is only for function not returning any value
1947+
1948+
1949+
// class comp into typescript; respect this order <Props, State>
1950+
class TypescriptComponent extends React.Component<{children: React.ReactNode}, {hasError: boolean}> {
1951+
state = { hasError: false };
1952+
render() {
1953+
if (this.state.hasError) return <p>Something went wrong</p>
1954+
return this.props.children
1955+
}
1956+
}
1957+
1958+
1959+
// function comp into typescript
1960+
function TypescriptComponent(props: {
1961+
title: string;
1962+
id: number;
1963+
hidden: boolean;
1964+
}) {
1965+
return <p key={props.id} hidden={props.hidden}>{props.title}</p>
1966+
}
1967+
1968+
// array of object prop into typescript
1969+
function TypescriptComponent(props: {
1970+
data: {
1971+
title: string;
1972+
intro: string;
1973+
}[];
1974+
}) {
1975+
return <p onClick={props.someFunc(props[0].title)}>{props[0].title}</p>
1976+
}
1977+
1978+
// function prop into typescript
1979+
function TypescriptComponent(props: {
1980+
title: string;
1981+
someFunc: (title: string) => void;
1982+
}) {
1983+
return <p onClick={props.someFunc(props.title)}>{props.title}</p>
1984+
}
1985+
1986+
// children prop into typescript
1987+
function TypescriptComponent({ children }: {children: React.ReactNode}) {
1988+
return <p>{children}</p>
1989+
}
1990+
1991+
// logic can also be extracted with interface {}
1992+
interface Props { children: React.ReactNode }
1993+
function TypescriptComponent({ children }: Props) {
1994+
return <p>{children}</p>
1995+
}
1996+
1997+
// interface is useful with complex typescript object
1998+
interface ComplexProp {
1999+
title: string;
2000+
data: {
2001+
id: number;
2002+
name: string;
2003+
}[];
2004+
optionalData?: [];
2005+
}
2006+
function TypescriptComponent(props: ComplexProp) {
2007+
return (
2008+
<>
2009+
<h1>{props.title}</h1>
2010+
{props.data.map(x => <p key={x.data}>{x.name}</p>)}
2011+
</>
2012+
)
2013+
}
2014+
2015+
2016+
const val: boolean = true; // TS will check if the type is correct
2017+
2018+
2019+
// use hooks in typescript
2020+
function TypescriptComponent() {
2021+
const [value, setValue] = useState<number>(0);
2022+
return <p onClick={() => setValue(value + 1)}>{value}</p>
2023+
}
2024+
2025+
// complex hook in typescript
2026+
function TypescriptComponent() {
2027+
const [text, setText] = useState<string | null>(null);
2028+
return <p onClick={() => setText('Hello')}>{text}</p>
2029+
}

0 commit comments

Comments
 (0)