【问题标题】:React-native unidentified is not an objectReact-native unidentified 不是对象
【发布时间】:2020-07-22 19:16:43
【问题描述】:

当我将删除操作发送到 Redux 时,我收到错误 unidentified is not an object evaluating selectedUser.imageUri 一切都从服务器加载,我知道删除操作有效,因为它从服务器中删除了对象,但是我收到此错误并且仅显示屏幕当我重新加载应用程序时更新。请有人可以帮助我,我真的需要你的帮助。提前谢谢你!!!我什至正在检查selecetedUser数组中是否没有对象,然后渲染一个名为nothing.png的图像

这是我看到错误的代码

const Viewer = (props) => {
    const userID = props.navigation.getParam('id')
    //Nothing is just a picture when there are no images
    import nothing from './Images/nothing.png'

    const selectedUser = useSelector(state => state.user.user.find(user => user.id === userID))
    const cBimageUri = {uri: selectedUser.imageUri }
    const checkImage = cBimageUri.length === 0? nothing : cBimageUri
    const cBimageUri = {uri: selectedUser.imageUri }

    const deleteCb = useCallback(() =>{
        dispatch(deleteUser(userID))
        props.navigation.goBack()
    },[userID])

    useEffect(() => { 
        props.navigation.setParams({deleteCb: deleteCb})
    },[deleteCb])

    return (
        <ScrollView style={{backgroundColor: 'white'}}>
            <Image source={checkImage} style={styles.image}/>
            <Text style={styles.name}>{selectedCookBook.name}</Text>
        </ScrollView>
    )
}

export default Viewer

Redux 减速器

import { CREATE_USER, DELETE_USER } from '../actions/account'

const initialState = {
    account: [],
}

const USerReducer = (state=initialState, action) =>{
    switch(action.type){

        case CREATE_USER:
            const newUser = new MyUser(
                action.userData.id,
                action.userData.name,
                action.userData.image, 
            )
            
            return { ...state, user: state.account.concat(newUser)}

        case DELETE_USER:
            const filteredItems = state.account.filter(cb => cb.id !== action.deleteCb)
            return {account: filteredItems }
 
        default: 
            return state
    }
}
export default USerReducer

Redux 操作

export const DELETE_COOKBOOK = 'CLEAR'
export const deleteCookbook = (deleteCb) => {
    return {type: DELETE_COOKBOOK, deleteCb: deleteCb}
}

控制台记录选定用户

[
    Object {
          "id": 1595444079901,
          "val": "Veveve",
    },

    name: John Snow,
    imageUri: 'file:///data/user/0/host.exp.exponent/cache/ExperienceData/%2540anonymous%252Frn-first-app-e648c632-2715-4169-abf3-e0cdbe2ac7d5/ImagePicker/461b63af-a908-47e9-8841-d5d8f2c4eb67.jpg
file:///data/user/0/host.exp.exponent/cache/ExperienceData/%2540anonymous%252Frn-first-app-e648c632-2715-4169-abf3-e0cdbe2ac7d5/ImagePicker/461b63af-a908-47e9-8841-d5d8f2c4eb67.jpg'
}
]

【问题讨论】:

  • 你能发布更多代码吗?因为不清楚什么是 selectedUser.imageUri
  • 我可以但选择的用户只是我的 redux-store 中的一个对象数组,在每个数组中呈现一个“id”、“图像”和“详细信息”
  • 我添加了附加代码以使其更清晰。我真的希望你能帮助我
  • 还是...你能做console.log(selectedUser)吗?
  • 感谢您迄今为止的帮助。我提供了控制台日志。 image.uri 是我的本地数据库中使用带有 redux-persist 的异步存储的位置。

标签: javascript react-native react-redux


【解决方案1】:

尝试改变关注

const Viewer = (props) => {
    const userID = props.navigation.getParam('id')
    //Nothing is just a picture when there are no images
    import nothing from './Images/nothing.png'

    const selectedUser = useSelector(state => state.user.user.find(user => user.id === userID))
    const cBimageUri = selectedUser.imageUri // --> changed it
    const checkImage = cBimageUri.length === 0? nothing : cBimageUri
    //const cBimageUri = {uri: selectedUser.imageUri } // is it a right row? just repeat previous 

    const deleteCb = useCallback(() =>{
        dispatch(deleteUser(userID))
        props.navigation.goBack()
    },[userID])

    useEffect(() => { 
        props.navigation.setParams({deleteCb: deleteCb})
    },[deleteCb])

    return (
        <ScrollView style={{backgroundColor: 'white'}}>
            <Image source={{uri: checkImage}} style={styles.image}/> // --> changed it. Not sure about it, if it's not working check below link
            <Text style={styles.name}>{selectedCookBook.name}</Text>
        </ScrollView>
    )
}

export default Viewer

此链接 https://stackoverflow.com/questions/50249353/uri-vs-url-in-react-native

【讨论】:

    猜你喜欢
    • 2018-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多