gpt4 book ai didi

javascript - react : How to check if received props are undefined?

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:40:03 27 4
gpt4 key购买 nike

您好,感谢您的宝贵时间:

我正在学习以下教程:https://app.pluralsight.com/library/courses/react-flux-building-applications/table-of-contents

目前我已经检测到,当您尝试创建作者时,它会尝试加载当前作者的 URL 的作者 ID。因为它正在创建,所以它是未定义的,因此创建作者功能不起作用。

我已经阅读了一些关于在 JS 和 React 中检查未定义类型的主题: How to determine if variable is 'undefined' or 'null'?

Checking for Undefined In React

https://github.com/facebook/react/issues/3725

我尝试了以下方法:

在 manageAuthorForm 中,我们将作者的状态传递给 authorForm:

  render() {
return (
<AuthorForm author={this.state.author}
onChange={this.setAuthorState}
onSave={this.saveAuthor}/>
);
};

然后我尝试进入 AuthorForm:

import React from 'react';
import {Input} from "../common/textInput";
import Link from "react-router-dom/es/Link";

class AuthorForm extends React.Component {
componentWillReceiveProps(nextProps) {
if (nextProps.state.author === undefined) {
this.props.author = {
author: {
id: '',
firstName: '',
lastName: '',

}
};
}
}

render() {
return (
<form>
<h1>Manage author</h1>
<Input
name="firstName"
label="First Name"
value={this.props.author.firstName}
onChange={this.props.onChange}
/>

<Input
name="lastName"
label="Last Name"
value={this.props.author.lastName}
onChange={this.props.onChange}
/>

<button type="submit" value="Save" className="btn btn-default" onClick={this.props.onSave}><Link
to="/authors">Save
author</Link>
</button>
</form>
);
}
}

export {AuthorForm}

它报告 nextProps 未定义:

Uncaught TypeError: Cannot read property 'author' of undefined
at AuthorForm.componentWillReceiveProps (authorForm.js:7)
at callComponentWillReceiveProps (react-dom.development.js:6389)
at updateClassInstance (react-dom.development.js:6575)
at updateClassComponent (react-dom.development.js:7848)
at beginWork (react-dom.development.js:8225)
at performUnitOfWork (react-dom.development.js:10224)
at workLoop (react-dom.development.js:10288)
at HTMLUnknownElement.callCallback (react-dom.development.js:542)
at Object.invokeGuardedCallbackDev (react-dom.development.js:581)
at invokeGuardedCallback (react-dom.development.js:438)
at renderRoot (react-dom.development.js:10366)
at performWorkOnRoot (react-dom.development.js:11014)
at performWork (react-dom.development.js:10967)
at requestWork (react-dom.development.js:10878)
at scheduleWorkImpl (react-dom.development.js:10732)
at scheduleWork (react-dom.development.js:10689)
at scheduleTopLevelUpdate (react-dom.development.js:11193)
at Object.updateContainer (react-dom.development.js:11231)
at react-dom.development.js:15226
at Object.unbatchedUpdates (react-dom.development.js:11102)
at renderSubtreeIntoContainer (react-dom.development.js:15225)
at Object.render (react-dom.development.js:15290)
at Object../src/index.js (index.js:17)
at __webpack_require__ (bootstrap 78b34f0f34b98a41c613:678)
at fn (bootstrap 78b34f0f34b98a41c613:88)
at Object.0 (authorStore.js:39)
at __webpack_require__ (bootstrap 78b34f0f34b98a41c613:678)
at bootstrap 78b34f0f34b98a41c613:724
at bootstrap 78b34f0f34b98a41c613:724
componentWillReceiveProps @ authorForm.js:7
callComponentWillReceiveProps @ react-dom.development.js:6389
updateClassInstance @ react-dom.development.js:6575
updateClassComponent @ react-dom.development.js:7848
beginWork @ react-dom.development.js:8225
performUnitOfWork @ react-dom.development.js:10224
workLoop @ react-dom.development.js:10288
callCallback @ react-dom.development.js:542
invokeGuardedCallbackDev @ react-dom.development.js:581
invokeGuardedCallback @ react-dom.development.js:438
renderRoot @ react-dom.development.js:10366
performWorkOnRoot @ react-dom.development.js:11014
performWork @ react-dom.development.js:10967
requestWork @ react-dom.development.js:10878
scheduleWorkImpl @ react-dom.development.js:10732
scheduleWork @ react-dom.development.js:10689
scheduleTopLevelUpdate @ react-dom.development.js:11193
updateContainer @ react-dom.development.js:11231
(anonymous) @ react-dom.development.js:15226
unbatchedUpdates @ react-dom.development.js:11102
renderSubtreeIntoContainer @ react-dom.development.js:15225
render @ react-dom.development.js:15290
./src/index.js @ index.js:17
__webpack_require__ @ bootstrap 78b34f0f34b98a41c613:678
fn @ bootstrap 78b34f0f34b98a41c613:88
0 @ authorStore.js:39
__webpack_require__ @ bootstrap 78b34f0f34b98a41c613:678
(anonymous) @ bootstrap 78b34f0f34b98a41c613:724
(anonymous) @ bootstrap 78b34f0f34b98a41c613:724

在网络浏览器中它报告:

×
TypeError: Cannot read property 'author' of undefined
AuthorForm.componentWillReceiveProps
C:/Users/YonePC/WebstormProjects/flux/src/components/authors/authorForm.js:7
4 |
5 | class AuthorForm extends React.Component {
6 | componentWillReceiveProps(nextProps) {
> 7 | if (nextProps.state.author === undefined) {
8 | this.props.author = {
9 | author: {
10 | id: '',
View compiled
▶ 15 stack frames were collapsed.
This screen is visible only in development. It will not appear if the app crashes in production.
Open your browser’s developer console to further inspect this error.

完整代码,在github:https://github.com/YoneMoreno/ReactFluxAuthorsPageTutorial

你能帮帮我吗?

谢谢;=)

最佳答案

简单的解决方案是:

<AuthorForm 
author={this.state.author || {firstName:'', lastName:'', id:''}}
...
/>

并从子组件中删除 componentWillReceiveProps 方法。

更好的方法将是:

1-author 的初始值赋给状态为:

this.state = {
author: {firstName:'', lastName:'', id:''}
}

2- 或者在子组件的render方法里面,这样写:

render(){

const {firstName='', lastName='', id=''} = this.props.author || {};

return(....)
}

并直接使用 firstName, lastName, id 而不是 this.props.


您的代码中的问题:

1- componentWillReceiveProps生命周期方法中应该是nextProps.author而不是nextProps.state.author

2- Props are read-only ,无论你将一个组件声明为一个函数还是一个类,它绝不能修改它自己的 props。您正在尝试更改 componentWillReceiveProps 中的值。

关于javascript - react : How to check if received props are undefined?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48789492/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com