react-native中列表为什么没有更新?

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中体现出来是什么原因呢?

阅读 950
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题