Introduction
MobX in React brings easy reactivity to your components. It can handle whole application state as well.
This site shows examples with React functional components and Hooks. Class components work in a similar way. In case there is class-specific behavior, we will mention it and supply an appropriate example.
Check out the example of the amazing Todo application or continue reading on why to observe or how to manage state.
Do you want to migrate toward Hooks? Have a look at our migration guide.
import React from 'react'
import { observer, useLocalStore } from 'mobx-react' // 6.x or mobx-react-lite@1.4.0
export const TodoList = observer<{ initialTodos: string[] }>(
({ initialTodos }) => {
const todoRef = React.useRef()
const store = useLocalStore(() => ({
todos: createTodos(initialTodos) as Record<string, boolean>,
get pendingTodos() {
return Object.keys(store.todos).filter(
todo => store.todos[todo] === false,
)
},
get doneTodos() {
return Object.keys(store.todos).filter(
todo => store.todos[todo] === true,
)
},
addTodo: () => {
store.todos[todoRef.current.value] = false
todoRef.current.value = ''
},
toggleTodo: (todo: string) => {
store.todos[todo] = !store.todos[todo]
},
}))
const renderTodo = (done: boolean) => todo => (
<Todo key={todo} done={done} text={todo} onToggle={store.toggleTodo} />
)
return (
<form onSubmit={store.addTodo}>
{store.pendingTodos.map(renderTodo(false))}
{store.doneTodos.map(renderTodo(true))}
<br />
<input ref={todoRef} />
<button>Add todo</button>
</form>
)
},
)
当前内容版权归 mobx-react 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 mobx-react .