let getName;
let get_name;
const GetName
10; // 10
10 + 30; // 40
const num = 50;
num; // 50
getName();
let x; // 변수 선언문
x = 4; // 할당문
function sayHello() { ... } // 함수 선언문
if (x) { ... } // 조건문
for (let i = 0; i < 4; i++) { ... } // 반복문
const todoState = atom({
key: 'todo',
default: []
});
function Item({ id, text }) {
return (
...
);
}
function TodoList() {
const [todos, setTodos] = useRecoilState(todoState);
...
return (
<>
...
<ul>
{todos.map(todo => (
<Item key={todo.id} {...todo} />
))}
</ul>
</>
)
}
// 새 프로젝트
npx create-react-app my-app --template typescript
// 기존 프로젝트에 추가
npm install --save typescript @types/node @types/react @types/react-dom @types/jest
Typescript에게 object의 shape를 설명!
// App.tsx
import Test from './components/Test';
export default function App() {
return (
<>
<Test text="hello" color="rosybrown" size="50" />
<Test color="royalblue" size="100" />
</>
);
}
// Test.tsx
import styled from 'styled-components';
interface TestProps {
text?: string; // string | undefined
color: string;
size: string;
}
interface ContainerProps {
color: string;
size: string;
}
const Container = styled.div<ContainerProps>`
font-size: ${(props) => props.size}px;
color: ${(props) => props.color};
`;
export default function Test({ text, color, size }: TestProps) {
return (
<Container color={color} size={size}>
{text}
</Container>
);
}
구글링과 공식문서 열심히..
function App() {
// (event) <- 'any' 무엇이든 될 수 있는 any type은 되도록 배제하기
const onClick = (event: React.FormEvent<HTMLButtonElement>) => {...}
const onInput = (event: React.FormEvent<HTMLSelectElement>) => {
console.log(event.currentTarget.value);
...
};
return (
<>
<form>
<button onClick={onClick}></button>
</form>
<select onInput={onInput}>
...
</select>
</>
)
}
export enum Days {
'Monday', // 0
'Tuesday' = 'Tuesday',
}
// styled.d.ts - extend override declaration file
import 'styled-components';
declare module 'styled-components' {
export interface DefaultTheme {
bgColor: string;
}
}
// theme.ts
import { DefaultTheme } from 'styled-components';
const theme: DefaultTheme = {
bgColor: 'whitesmoke';
}
// index.tsx
import ReactDOM from 'react-dom';
import { ThemeProvider } from 'styled-components';
import App from './App';
import { theme } from './theme';
ReactDOM.render(
<ThemeProvider theme={theme}>
<App />
</ThemeProvider>,
document.getElementById('root')
);
// App.js
import styled from 'styled-components';
const Container = styled.div`
background-color: ${(props) => props.theme.bgColor};
`;
export default function App() {
return <Container></Container>;
}
import { useRecoilState, useRecoilValue } from 'recoil';
import { firstState, firstSelector, secondState } from '../atoms';
function App() {
const firstValue = useRecoilValue(firstState);
const selectorValue = useRecoilValue(firstSelector);
const [secondValue, setSecondValue] = useRecoilState(secondState);
...
}
// atoms.js
export const firstState = atom({
key: 'uniqKey',
default: []
});
export const firstSelector = selector({
key: 'uniqKey',
get: ({ get }) => {
const firsrtAtom = get(firstState);
// return 값이 firstSelector의 value
return ...;
}
})
// App.js
import { useRecoilValue } from 'recoil';
import { firstSelector } from '../atoms';
funciton App() {
const firstValue = useRecoilValue(firstSelector);
...
}
Immutability은 함수형 프로그래밍의 핵심 원리로 react의 state 변경 시 항상 새로운 state를 만들어 반환.
코드가 지저분해도 일단 작동하게 만들고 나중에 리팩토링!!