chenhuiYj / chenhuiYj/note

react - mobx

Open
#53 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
HTML
Stars
6
Forks
0
PR merge metrics
No merged PRs in 30d

Description

1.概念

mobx 通过透明的函数响应式编程(transparently applying functional reactive programming - TFRP)使得状态管理变得简单和可扩展。

2.举例

// src/stores/todoStore.js
import { useLocalObservable } from 'mobx-react-lite';

const useTodoStore = () => {
  const todos = useLocalObservable(() => ({
    items: [],

    addTodo(text) {
      if (text.trim()) {
        this.items.push({
          id: Date.now(),
          text,
          completed: false,
        });
      }
    },

    removeTodo(id) {
      this.items = this.items.filter(todo => todo.id !== id);
    },

    toggleTodo(id) {
      const todo = this.items.find(todo => todo.id === id);
      if (todo) {
        todo.completed = !todo.completed;
      }
    },
  }));

  return todos;
};

export default useTodoStore;
// src/components/TodoList.js
import React, { useState } from 'react';
import useTodoStore from '../stores/todoStore';

const TodoList = () => {
  const todos = useTodoStore();
  const [input, setInput] = useState('');

  const handleAddTodo = () => {
    if (input.trim()) {
      todos.addTodo(input);
      setInput('');
    }
  };

  return (
    <div>
      <h1>Todo List</h1>
      <input
        type="text"
        value={input}
        onChange={(e) => setInput(e.target.value)}
        onKeyPress={(e) => {
          if (e.key === 'Enter') {
            handleAddTodo();
          }
        }}
      />
      <button onClick={handleAddTodo}>Add Todo</button>
      <ul>
        {todos.items.map(todo => (
          <li key={todo.id}>
            <input
              type="checkbox"
              checked={todo.completed}
              onChange={() => todos.toggleTodo(todo.id)}
            />
            {todo.text}
            <button onClick={() => todos.removeTodo(todo.id)}>Remove</button>
          </li>
        ))}
      </ul>
    </div>
  );
};

export default TodoList;
// src/App.js
import React from 'react';
import TodoList from './components/TodoList';

const App = () => {
  return (
    <div>
      <TodoList />
    </div>
  );
};

export default App;

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue presents a MobX example using src/stores/todoStore.js, src/components/TodoList.js, and src/App.js, but names no documentation target or requested change. Start by reviewing the repository's existing note structure and confirming where this content belongs; done means the React/MobX example is placed in the agreed location and renders consistently with the surrounding notes.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, react
Domain
documentation
Issue type
Documentation
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.