mobx 有bug 的 todolist (待修改)

Posted 胡椒

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mobx 有bug 的 todolist (待修改)相关的知识,希望对你有一定的参考价值。

import React, { Component, useEffect } from \'react\';
import \'./App.css\';
import { observable, computed, action } from \'mobx\';
import { observer,Provider,inject } from \'mobx-react\'

//数据结构
class Todo {

@observable todos = [
    {
        id: 1,
        title: \'任务1\',
        finished: true
    }, {
        id: 2,
        title: \'任务2\',
        finished: false
    }
];

@computed get unfinishedTodoCount() {
    return this.todos.filter(todo => !todo.finished).length;
}

@action delTodo(id) {
    console.log(\'删除\');
    this.todos = this.todos.filter(todo => todo.id !== id);
    console.log(this.todocs);
}

@action updateTodo(id,done) {
    console.log(\'update\');
    const newTodos = this.todos.map((todoObj)=>{
        if(todoObj.id === id) return {...todoObj,done}
        else return todoObj
    })
    this.todos = newTodos;
}

}

@observer
class TodoListView extends Component {

updateTodo = (todoId,done) => {
    this.props.todoList.delTodo(todoId,done)
}
delTodo = (todoId) => {
    this.props.todoList.delTodo(todoId)
}
render() {
    return (
        <div>
            <ul>
                {this.props.todoList.todos.map(todo =>
                    <TodoView todo={todo} key={todo.id} delTodo={this.delTodo} updateTodo={this.updateTodo} />
                )}
            </ul>
            未完成任务数:{this.props.todoList.unfinishedTodoCount}
        </div>
    )

}

}

@observer
class TodoView extends Component {

render() {
    // return <div>{this.props.todo.title}</div>;
    var todo = this.props.todo;
    return (
        <li>
            <label htmlFor="">
                <input
                    type="checkbox"
                    checked={todo.finished}
                    onChange={() => this.props.updateTodo(todo.id,todo.finished)}
                />

                {todo.title}
            </label>
            <button onClick={() => this.props.delTodo(todo.id)}>del</button>
        </li>
    )
}

}

const todoList = new Todo();

const ToDoApp = inject(\'todoList\')(observer(({todoList}) => {

useEffect(() => {
    console.log(\'hhhh\');
})
return (
    <div>
        <TodoListView todoList={todoList}/>
    </div>
)

}))

class App extends Component{

render(){
    return (
        <Provider todoList={todoList}>
            <div>
                <ToDoApp />
            </div>
        </Provider>
        
    );
}

}
export default App;

以上是关于mobx 有bug 的 todolist (待修改)的主要内容,如果未能解决你的问题,请参考以下文章

类组件使用mobx实现数据修改以及请求数据

Mobx与Redux的异同

淘宝bug bug bug

第七讲、Vue3.x 实现一个完整的toDoList(待办事项)

AntDesign(React)学习-11 简单使用mobx

react+mobx 兼容IE11