Woongjae Lee
NHN Dooray - Frontend Team
2woongjae@gmail.com
2018.02
const { Map } = require("immutable");
const map1 = Map({ a: 1, b: 2, c: 3 });
const map2 = map1.set('b', 50);
map1.get('b') + " vs. " + map2.get('b') // 2 vs. 50
const { List } = require('immutable')
const list1 = List([ 1, 2 ]);
const list2 = list1.push(3, 4, 5);
const list3 = list2.unshift(0);
const list4 = list1.concat(list2, list3);
assert.equal(list1.size, 2);
assert.equal(list2.size, 5);
assert.equal(list3.size, 6);
assert.equal(list4.size, 13);
assert.equal(list4.get(0), 1);
import { BuildActions } from '../actions';
import * as types from '../constants';
export interface Build {
text: string;
completed: boolean;
id: number;
}
const initialState: Build[] = [];
export interface Builds {
builds: Build[];
}
export function builds(state: Build[] = initialState, action: BuildActions) {
switch (action.type) {
case types.ADD_BUILD:
return [
...state,
{
id: state.reduce((maxId, todo) => Math.max(todo.id, maxId), -1) + 1,
completed: false,
text: action.text
}
];
case types.DELETE_BUILD:
return state.filter(todo => todo.id !== action.id);
default:
return state;
}
}
import { BuildActions } from '../actions';
import * as types from '../constants';
import { List } from 'immutable';
export interface Build {
text: string;
completed: boolean;
id: number;
}
const initialState: Build[] = [];
export interface Builds {
builds: Build[];
}
export function builds(state: Build[] = initialState, action: BuildActions) {
switch (action.type) {
case types.ADD_BUILD:
const newState: List<Build> = List(state).push({
id: state.reduce((maxId, todo) => Math.max(todo.id, maxId), -1) + 1,
completed: false,
text: action.text
});
console.log(newState);
return newState;
case types.DELETE_BUILD:
return List(state).filter(todo => {
if (todo === undefined) {
return false;
}
return todo.id !== action.id;
});
default:
return state;
}
}
~/Project/workshop-201801
➜ npx create-react-app immutable-ts-pure-component --scripts-version=react-scripts-ts
~/Project/workshop-201801 took 1m 47s
➜ cd immutable-ts-pure-component
Project/workshop-201801/immutable-ts-pure-component is 📦 v0.1.0 via ⬢ v8.9.4
➜ npm i immutable -D
+ immutable@3.8.2
added 1 package in 7.387s
import * as React from 'react';
import './App.css';
import Pure from './Pure';
const logo = require('./logo.svg');
class App extends React.Component<{}, { count: number }> {
state = {
count: 0
};
render() {
return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<h1 className="App-title">Welcome to React</h1>
</header>
<p className="App-intro">
<button
onClick={() => this.setState({ count: this.state.count + 1 })}
>
카운트 업
</button>
</p>
<p>{this.state.count}</p>
<Pure {...this.state} />
</div>
);
}
}
export default App;
import * as React from 'react';
interface PureProps {
count: number;
}
class Pure extends React.Component<PureProps, {}> {
shouldComponentUpdate(prevProps: PureProps, prevState: {}) {
if (prevProps.count > 20) {
return true;
}
return false;
}
render() {
return <div>{JSON.stringify(this.props)}</div>;
}
}
export default Pure;
By Woongjae Lee
코드버스킹 워크샵 - React with TypeScript 세번째 (2018년 1월 버전)