index,未定义???写的,delItem传参
来源:6-12 使用React-redux完成TodoList功能
慕粉1473597318
2021-06-20
老师代码:
Todolist里代码,只看,点击list中的item,会自动删除,报错说我index未定义
import {Component } from 'react’
import {connect} from ‘react-redux’
class TodoList extends Component {
render(){
return (
提交
{this.props.list.map((item,index)=>{
return<li onClick={()=>this.props.handleDelItem(index)} key={index}>{item}
})}
)
}
// handleInputChange(e){
// console.log(e.target.value)
// }
}
//map映射
//固定接受一个state,来自store的数据
const mapStateToProps = (state)=>{
return {
inputValue: state.inputValue,
list: state.list
}
}
//store.dispatch
//修改数据
const mapDispatchToProps = (dispatch)=>{
return{
changeInputValue(e){
//console.log(e.target.value)
const action ={
type: ‘change_input_value’,
value: e.target.value
}
//dispatch 把action发给store,store会去reducer里面查询记录
dispatch(action);
},
handleClick(){
//console.log(‘123’)
const action ={
type: ‘add_item’
}
dispatch(action)
},
handleDelItem(index){
const action ={
type: ‘del_item’,
index
}
dispatch(action)
}
}
}
//export default TodoList;
export default connect(mapStateToProps,mapDispatchToProps)(TodoList);
//reducer.js
代码
const defaultState = {
inputValue: ‘’,
list: []
}
export default (state=defaultState ,action)=>{
if(action.type === ‘change_input_value’){
const newState = JSON.parse(JSON.stringify(state))
newState.inputValue = action.value
return newState
}
if(action.type === ‘add_item’){
const newState = JSON.parse(JSON.stringify(state))
newState.list.push(newState.inputValue)
newState.inputValue =’'
return newState;
}
if(action.type === ‘del_item’){
const newState = JSON.parse(JSON.stringify(state))
newState.list.splice(index,1)
return newState;
}
return state;
}
报错截图:
请问老师如何修正???
我写了index,但是提示bug
1回答
-
Dell
2021-06-21
action.index
012021-06-21
相似问题