Javascript 无法读取未定义的属性“字符串” | React.PropTypes | LayoutPropTypes.js

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44573199/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 02:37:07  来源:igfitidea点击:

Cannot read property 'string' of undefined | React.PropTypes | LayoutPropTypes.js

javascriptreactjsreact-nativenpm

提问by Nicolas Meienberger

After deleting and reinstalling my node_modules folder, I'm facing an issue that I don't understand in LayoutPropTypes.js file.

删除并重新安装我的 node_modules 文件夹后,我在 LayoutPropTypes.js 文件中遇到了一个我不明白的问题。

In node_modules/react-native/Libraries/StyleSheet/LayoutPropTypes.jsThe following variable is undefined : var ReactPropTypes = require('React').PropTypes;

node_modules/react-native/Libraries/StyleSheet/LayoutPropTypes.js以下变量未定义:var ReactPropTypes = require('React').PropTypes;

react-native: 0.45.1 react: 16.0.0-alpha.12

反应原生:0.45.1 反应:16.0.0-alpha.12

回答by Chase DeAnda

React.PropTypes is now deprecated:

React.PropTypes 现在已弃用:

Note: React.PropTypes is deprecated as of React v15.5. Please use the prop-types library instead.

注意:React.PropTypes 从 React v15.5 开始被弃用。请改用 prop-types 库。

You need to add the prop-typespackage separately now. The error most likely just started to show up because you deleted your node_modulesfolder and then reinstalled everything which upgraded your reactversion.

您现在需要单独添加prop-types包。该错误很可能刚刚开始出现,因为您删除了node_modules文件夹,然后重新安装了升级react版本的所有内容。

回答by lomse

React is no more shipped with PropTypes. You will need to install it.

React 不再附带 PropTypes。您将需要安装它。

First install the prop-typespackage by running npm i prop-types --save.

首先prop-types通过运行安装包npm i prop-types --save

Next use the prop-typespackage into your component like this:

接下来prop-types像这样在你的组件中使用这个包:

import React from 'react'
import PropTypes from 'prop-types'

export const AwesomeComponent = props => {
    return(
        <h1>Hello {props.name}</h1>
    )
}

AwesomeComponent.propTypes = {
    name: PropTypes.string.isRequired
}

Or simply use an interface if you are using Typescript like this:

或者,如果您使用的是这样的 Typescript,则只需使用一个接口:

import * as React from 'react'

interface IAwesomeComponentProps {
    name: string
} 

export const AwesomeComponent: React.FC<IAwesomeComponentProps> = props => {
    return(
        <h1>Hello {props.name}</h1>
    )
}

回答by Christoffer Karlsson

Are you absolutely sure you are using react 16.0.0-alpha.12?

你绝对确定你正在使用 react16.0.0-alpha.12吗?

Check your package.jsonif you have a ^before the react version, if you have, it probably have installed the latest react version, which currently is 16.0.0-alpha.13, in which it breaks as you say (just had the problem myself). Having the ^before the version, allows it to install newer minor and patch versions. You can read more about it here.

检查你的package.json如果你有一个^之前的反应版本,如果你有,它可能已经安装了最新的反应版本,目前是16.0.0-alpha.13,它如你所说的那样中断(只是我自己有问题)。拥有^之前的版本,允许它安装更新的次要版本和补丁版本。您可以在此处阅读更多相关信息

To keep it at the exact version you specify, simply remove the ^before the version, so that your package.json looks like this:

要将其保持在您指定的确切版本,只需删除^版本之前的 ,以便您的 package.json 看起来像这样:

  "dependencies": {
    "react": "16.0.0-alpha.12",
    "react-native": "0.45.1",
  }

Remember to re-install your node_modules after your changes.

请记住在更改后重新安装 node_modules。

回答by skd

I have the similar problem, no solution. Answers talking about 'prop-types' package are meaningless, the problem is come from react-native source code. That is not an option to manually fix react native source in node_modules folder.

我有类似的问题,没有解决方案。谈论“prop-types”包的答案毫无意义,问题来自于 react-native 源代码。这不是在 node_modules 文件夹中手动修复 react native 源的选项。

回答by Erick Garcia

Now you can update to react-native 0.46.4, just follow along this guide: https://facebook.github.io/react-native/docs/upgrading.html

现在您可以更新到 react-native 0.46.4,只需按照本指南进行操作:https://facebook.github.io/react-native/docs/upgrading.html

my package.json looks like this:

我的 package.json 看起来像这样:

"react": "16.0.0-alpha.12",
"react-native": "0.46.4",

回答by nedam limboo

instead of defining the value for component as like this: propName: React.PropTypes.string

而不是像这样定义组件的值:propName: React.PropTypes.string

DEFINE LIKE THIS propName: PropTypes.string

像这样定义 propName: PropTypes.string

and finally save it.

最后保存它。