出现以下错误(TypeError:undefined 不是对象(正在评估'props.route.params.contactId))
2020-06-19
71
我在以下代码中遇到了 TypeError:
if (typeof props.route.params != "undefined") {
我尝试检查
props.route.params
是否存在,如果存在,我将尝试从 redux 获取和过滤数据。
在 JS 中
props.route.params
不会触发 TypeError,但在这里不起作用,有解决方法吗?
const EditContactScreen = props => {
let contactId, editedContact;
const [nameDropdown, setNameDropdown] = useState(false);
const [name, setName] = useState();
const [phone, setPhone] = useState();
const [email, setEmail] = useState();
const [address, setAddress] = useState();
const { navigation } = props;
if (typeof props.route.params != "undifined") {
contactId = props.route.params.contactId;
editedContact = useSelector(state => {
for(let i = 0; i < state.contacts.length; i++){
if(contactId === state.contacts[i].id){
return state.contacts[i];
}
}
});
}
2个回答
const EditContactScreen = props => {
let contactId, editedContact;
const [nameDropdown, setNameDropdown] = useState(false);
const [name, setName] = useState();
const [phone, setPhone] = useState();
const [email, setEmail] = useState();
const [address, setAddress] = useState();
const { navigation } = props;
if ('contactId' in props.route.params) {
contactId = props.match.contactId;
editedContact = useSelector(state => {
for(let i = 0; i < state.contacts.length; i++){
if(contactId === state.contacts[i].id){
return state.contacts[i];
}
}
});
}
这应该可行
Sudhanshu Kumar
2020-06-19
问题是我在以下行
typeof props.route.params != "undifined"
中拼错了“undefined”。更正后,一切正常。
M Masood
2020-06-25