Home > front end >  Is to react - story, I want to set a delete function, does not work. According to always read the pr
Is to react - story, I want to set a delete function, does not work. According to always read the pr

Time:09-21

Import the React, {Component} from 'React';
The import * as uuid from 'uuid;
The import {render} from '@ testing - library/react;


The class TodoList extends ponent {
React.Com
State={
Text: "'
}
OnChange=(e)=& gt; {
Enclosing setState ({
Text: e. arget. Value,
})
}
The onClick=()=& gt; {
Const id=uuid. V4 ();
This. Props. AddTodo (id, this. State. The text)
}

OnDelete ()==& gt; {

This. Props. DeteleTodo (this. Props. Todos. Id)}

Render () {

Return (


    {this. Props. Todos. The map ((todo)=& gt; (


  • )

    )}




)

}
}
Export the default TodoList

Here is my actions/add - todo. Js

Export const addTodo=(id, text)=& gt; ({
Type: 'ADD_TODO',
Content: {id, text},
});
Export const deteleTodo=(id)=& gt; ({
Type: 'REMOVE_TODO',
Content: id
})
Here is my containers/todo list. Js

The import {addTodo, deteleTodo} from '../actions/add - todo '

Const mapStateToProps=(state)=& gt; ({
Todos: state. Todos,
})
Const mapDispatchToProps=(dispatch)=& gt;
{
Return {
AddTodo: (id, text)=& gt; Dispatch (addTodo (id, text),
DeteleTodo (id)=& gt; Dispatch (deteleTodo (id))
}
}
Export the default connect (mapStateToProps mapDispatchToProps) (TodoList)
Here is my reducers index and todos. Js

Export the default (state=[], the action)=& gt; {
Switch (the action type) {
Case 'ADD_TODO:
Return [... the state, the action payload];
Case 'REMOVE_TODO:
Const newState because=state. The filter ((todo)=& gt; Todo. Id!==action. Content. Id);
Return newState because;
Default:
Return the state;
}
}

The import {combineReducers} from the 'story'
The import todosReducer from '/todos'

Export the default combineReducers ({
Todos: todosReducer,
})

CodePudding user response:

Modified the todoList component code

 
Import the React from 'React';
The import * as uuid from 'uuid;


The class TodoList extends ponent {
React.ComThe constructor (props) {
Super (props)
This. OnChange=this. OnChange. Bind (this)
Enclosing the onClick=this. The onClick. Bind (this)
Enclosing onDelete=this. OnDelete. Bind (this)
This. State={
Text: "'
}
}

OnChange (e)=& gt; {
Enclosing setState ({
Text: e. arget. Value,
})
}
The onClick ()=& gt; {
Const id=uuid. V4 ();
This. Props. AddTodo (id, this. State. The text)
}

OnDelete ()=& gt; {
This. Props. DeteleTodo (this. Props. Todos. Id)
}

Render () {

Return (


    {this. Props. Todos. The map ((todo)=& gt; (


  • )

    )}




)

}
}
Export the default TodoList
  • Related