2woongjae@gmail.com
2017.07
import {observable} from 'mobx';
export class AgeState {
@observable private _age = 35;
constructor(age: number) {
this._age = age;
}
public getAge(): number {
return this._age;
}
public setAge(age: number): void {
this._age = age;
}
public addAge(): void {
this._age = this._age + 1;
}
}
import * as React from 'react';
import './App.css';
import {AgeState} from './AgeState';
import {observer} from 'mobx-react';
const logo = require('./logo.svg');
const ageState = new AgeState(35);
@observer class App extends React.Component<{}, {}> {
constructor(props: {}) {
super(props);
this.addAge = this.addAge.bind(this);
}
render() {
return (
<div className="App">
<div className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<h2>Welcome to React</h2>
</div>
<p className="App-intro">
{ageState.getAge()}
<button onClick={() => this.addAge()}>한해가 지났다.</button>
</p>
</div>
);
}
addAge() {
const age = ageState.getAge();
ageState.setAge(age + 1);
// ageState.addAge();
}
}
export default App;
import { observable } from 'mobx';
const list = observable([1, 2, 4]);
const isLogin = observable(true);
const age = observable({
age: 35
});
class AgeState {
@observable
private _age = 35;
}
const ageState = new AgeState();
const StatelessApp = observer(() => {
function addAge(): void {
ageState.addAge();
}
return (
<div>
{ageState.getAge()}
<button onClick={() => addAge()}>한해가 지났다.</button>
</div>
);
});
@observer
class App extends React.Component<{}, {}> {
constructor(props: {}) {
super(props);
this.addAge = this.addAge.bind(this);
}
render() {
return (
<div>
{ageState.getAge()}
<button onClick={() => this.addAge()}>한해가 지났다.</button>
</div>
);
}
addAge() {
ageState.addAge();
}
}
class AgeState {
constructor() {
extendObservable(this, {
_age: 35,
age: computed(function() {
return (this._age > 40) ? this._age : 0;
})
})
}
}
class AgeState {
@observable private _age: number = 35;
@computed
get age(): number {
return (this._age > 40) ? this._age : 0;
}
}
ref.child('todos').on('value', action((snapshot: firebase.database.DataSnapshot) => {
if (snapshot) {
const list = snapshot.val();
const todos = [];
if (list !== null) {
for (const key of Object.keys(list)) {
todos.push({
id: key,
text: list[key]
});
}
}
this.todos = todos;
}
}));
@action addTodo = (text: string) => {
const ref = db.ref();
ref.child('todos').push().set(text);
}
ReactDOM.render(
<Provider store={ageState}>
<App />
</Provider>,
document.getElementById('root') as HTMLElement
);
@inject('store')
@observer
class App extends React.Component<{ store?: IAgeState; }, {}> {
render() {
const store = this.props.store as IAgeState;
return (
<div className="App">
<DevTools />
<p className="App-intro">
{store.age}
<button onClick={() => store.addAge()}>한해가 지났다.</button>
<button onClick={() => store.addAgeAsync()}>깃헙 비동기 호출</button>
</p>
</div>
);
}
}
import { observable } from 'mobx';
export class TodoStore {
@observable todos = [];
}
export default new TodoStore();
import {Provider} from 'mobx-react';
import todoStore from './stores/todoStore';
ReactDOM.render(
<Provider todoStore={todoStore} >
<App />
</Provider>,
document.getElementById('root') as HTMLElement
);
import * as React from 'react';
import { observer } from 'mobx-react';
interface TodoInputProps {
addTodo(text: string): void;
}
@observer
class TodoInput extends React.Component<TodoInputProps, {}> {
private _input: HTMLInputElement;
render() {
return (
<div>
<input type="text" ref={ref => this._input = ref as HTMLInputElement} />
<button onClick={() => this._addTodo()}>저장</button>
</div>
);
}
_addTodo = () => {
const input = this._input;
if (input.value !== '') {
this.props.addTodo(input.value);
input.value = '';
}
}
}
export default TodoInput;
import * as React from 'react';
import { observer } from 'mobx-react';
interface TodoListProps {
todos: { id: string; text: string; }[];
deleteTodo(id: string): void;
}
@observer
class TodoInput extends React.Component<TodoListProps, {}> {
render() {
const list = this.props.todos.map(todo => (
<li key={todo.id}>
{todo.text}
<button onClick={() => this.props.deleteTodo(todo.id)}>삭제</button>
</li>
));
return (
<div>
<ul>{list}</ul>
</div>
);
}
}
export default TodoInput;
{
"rules": {
".read": true,
".write": true
}
}
import { observable, action } from 'mobx';
import * as firebase from 'firebase';
const config = {
apiKey: 'AIzaSyD4Ywl6RRaFrSy8ZXL10hsSl6orA2PF5hc',
authDomain: 'mobx-ts-todo.firebaseapp.com',
databaseURL: 'https://mobx-ts-todo.firebaseio.com',
projectId: 'mobx-ts-todo',
storageBucket: 'mobx-ts-todo.appspot.com',
messagingSenderId: '900175359555'
};
firebase.initializeApp(config);
const db: firebase.database.Database = firebase.database();
export class TodoStore {
@observable todos: { id: string; text: string; }[] = [];
constructor() {
const ref = db.ref();
ref.child('todos').on('value', action((snapshot: firebase.database.DataSnapshot) => {
if (snapshot) {
const list = snapshot.val();
const todos = [];
if (list !== null) {
for (const key of Object.keys(list)) {
todos.push({
id: key,
text: list[key]
});
}
}
this.todos = todos;
}
}));
}
@action addTodo = (text: string) => {
const ref = db.ref();
ref.child('todos').push().set(text);
}
@action deleteTodo = (id: string) => {
const ref = db.ref();
ref.child('todos').child(id).remove();
}
}
export default new TodoStore();