我有一個簡單的應用程序與react/redux構建。在我的「TODO」-app中,我可以添加項目並對其進行過濾。我只是按照this example。但我也嘗試添加一個實際刪除項目的方法,但它似乎不起作用。刪除與TODO「TODO」
操作:
let nextTodoId = 0
export const addTodo = text => {
return {
type: 'ADD_TODO',
id: nextTodoId++,
text
}
}
export const setVisibilityFilter = filter => {
return {
type: 'SET_VISIBILITY_FILTER',
filter
}
}
export const toggleTodo = id => {
return {
type: 'TOGGLE_TODO',
id
}
}
export const deleteTodo = id => {
return {
type: 'DELETE_TODO',
id: id
}
}
減速機:
const todos = (state = [], action) => {
switch (action.type) {
case 'ADD_TODO':
return [
...state,
{
id: action.id,
text: action.text,
completed: false
}
];
case 'TOGGLE_TODO':
return state.map(todo =>
(todo.id === action.id)
? {...todo, completed: !todo.completed}
: todo
);
case 'DELETE_TODO':
return state.filter(todo => todo.id !== action.id);
default:
return state
}
}
export default todos
而且在我的容器:
import React from 'react'
import {connect} from 'react-redux'
import {deleteTodo} from '../actions'
let RemoveTodo = ({dispatch}) => {
return (
<div>
<a onClick={e => {
e.preventDefault()
// dispatch(deleteTodo())
console.log(dispatch(deleteTodo()));
}}>Remove TODO</a>
</div>
)
}
RemoveTodo = connect()(RemoveTodo)
export default RemoveTodo
組件:
import React from 'react'
import PropTypes from 'prop-types'
import {connect} from 'react-redux'
import RemoveTodo from '../containers/RemoveTodo'
export default class Todo extends React.Component {
constructor(props) {
super(props);
this.state;
}
render() {
return (
<li
onClick={this.props.onClick}
className="list-group-item justify-content-between border-c1 c1"
>
<span>{this.props.text} {this.props.completed ? <i className="fa fa-check"/> : null}</span>
<span className="badge c1-bg badge-pill">
<RemoveTodo />
</span>
</li>
)
}
}
Todo.propTypes = {
onClick: PropTypes.func.isRequired,
completed: PropTypes.bool.isRequired,
text: PropTypes.string.isRequired,
id: PropTypes.number.isRequired
}
出於某種原因,我的 「調度(deleteTodo())」 沒有找到ID ...當我登錄,我只是得到一個對象:類型: 「DELETE_TODO」,ID:不確定。爲什麼?我在這裏錯過了什麼?我是新來的還原/反應。在此先感謝...
編輯:
當我通過在標識加入訊(deleteTodo(ID))我得到這個錯誤:
'訊(deleteTodo()) '缺少'id':'dispatch(deleteTodo(id))' –
你沒有通過這個ID .. –
@TomFenech我試過了,但後來我在控制檯中得到另一個錯誤,說「id沒有定義「或者其他什麼...... – maverick