javascript - React/Redux,从模型调用方法抛出 TypeError
问题描述
我正在尝试从模型运行 formatAddress,以格式化的方式显示地址,但只要调用该方法,它就会抛出 TypeError。
零件
export const PostRow: React.FC<Props> = ({ index, userIndex }) => {
const dispatch = useDispatch()
const { name, username, email, formatAddress } = useSelector((state: RootState) => state.users[userIndex])
console.log(formatAddress())
减速器
export const userReducer: Reducer<UserState, UserActions> = (state: UserState = [], action: UserActions): UserState => {
switch (action.type) {
case UserActionTypes.GET_ALL_USERS:
return action.payload.map((user: UserResponseDTO) => (new User(user)))
default:
return state
}
}
模型
export class User {
public id: number
public name: string
public username: string
public email: string
public address: UserAdress
public phone: string
public website: string
public company: UserCompany
constructor(userDTO: UserResponseDTO) {
this.id = userDTO.id
this.name = userDTO.name
this.username = userDTO.username
this.email = userDTO.email
this.address = userDTO.address
this.phone = userDTO.phone
this.website = userDTO.website
this.company = userDTO.company
}
formatAddress() {
return `${this.address.street}. ${this.address.city} - ${this.address.zipcode}`
}
}
退货
TypeError: Cannot read property 'address' of undefined
解决方案
对于常规函数, 的值this
取决于函数的调用方式。由于您已将 formatAddress 保存到变量中,然后使用 code 调用它formatAddress()
,因此在没有任何上下文的情况下调用它。结果,this
默认为未定义(或在非严格模式下,为窗口对象)。
一种选择是更改您的调用方式。如果你保存用户对象然后调用user.formatAddress()
,那么this
将被设置为等于用户:
const user = useSelector((state: RootState) => state.users[userIndex]);
console.log(user.formatAddress());
或者,您可以将 formatAddress 绑定到用户。这将创建一个新函数,其值this
永久存在:
constructor(userDTO: UserResponseDTO) {
this.id = userDTO.id
// ... etc
this.company = userDTO.company
this.formatAddress = this.formatAddress.bind(this);
}
或者使用箭头函数,因为它们是this
从this
创建箭头函数时获得的。由于您使用的是打字稿,因此可以使用以下语法:
export class User {
// ... stuff omitted
constructor(userDTO: UserResponseDTO) {
//...
}
formatAddress = () => {
return `${this.address.street}. ${this.address.city} - ${this.address.zipcode}`
}
}
推荐阅读
- python - 使用 .bat 文件中的“调用”to.py 脚本时出错
- odoo - 单击保存按钮后odoo服务器错误
- r - 如果所有值都符合 R 中的条件,则选择行
- python-3.x - 将两列数据合二为一,不影响数据值
- com - 如何使用 Redemption,它独立于其他已安装的包含自己的 Redemption 版本的软件?
- docker - 如何在 Ubuntu 的 Docker 环境中获取默认 nginx 配置文件的内容
- javascript - 如何在javascript中的heremaps中创建折线的实例?
- apache-spark - 广播和重新分区的数据帧的范围是什么?
- xml - 通过 XML 数据提供者的 Acumatica XML 导入(或导出)
- javascript - 使用 pug、js、json 加载图像