const myReducer = (state,action)=>{
switch(action.type){
case 'ADD':
Alert.alert('123',action.val)
return {
...state,
state: state.list.concat([{
content:action.val,
state: false
}])
}
break;
case 'DEL':
return {
...state,
state: state.list.splice(action.target,1)
}
break;
case 'CHANGE_STATE':
return {
...state,
state: state.list[action.aim].state=!state.list[action.aim].state
}
break;
default:
return state
}
}
const Todos =()=>{
const [state,dispatch]=useReducer(myReducer,{
list:[
{
content:'吃饭',
state: false
},
{
content:'做饭',
state: false
},
{
content:'买菜',
state: false
}
]})
function handleAdd(e){
dispatch({type:'ADD',val:e.nativeEvent.text})
}
return (
<View>
<Text>todolist</Text>
<View style={{borderWidth:1,borderColor:'#999'}}>
<TextInput onSubmitEditing={(e)=>handleAdd(e)}/>
</View>
<List list={state.list}/>
</View>
)
}
const List= (props)=>{
const {list}=props
return (
<View>
<Text>{list[1].content}</Text>
<FlatList
data={
list
}
renderItem={
({item,index}) => {return(
<Text>{item.content}</Text>
)}
}
/>
</View>
)
}
export default Todos;
reducer接收到了action并且也有数据,但是没有在List中体现出来是什么原因呢?