道具类型在反应应用程序中不起作用 [英] prop types not working in react application

查看:46
本文介绍了道具类型在反应应用程序中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的 react-redux 应用程序中使用 proptypes,但它不起作用,或者我做错了什么.这是代码示例的结尾:

LoginPage.propTypes = {login_form: PropTypes.string};函数 mapStateToProps(state) {返回 { loginPage: state.loginPage }}导出默认连接(mapStateToProps)(登录页面);

login_form 是布尔值,我特意写了 PropType.string 来查看它是否工作,但它没有给我任何错误,这可能是因为 redux connect,但我无法搜索任何相关内容.请任何人告诉我我做错了什么:/谢谢.

解决方案

来自

或者,您可能一定要检查default.

I am trying to use proptypes for in my react-redux app, but it is not working, or I am doing something wrong. this is end of code example:

LoginPage.propTypes = {
     login_form: PropTypes.string
};

function mapStateToProps(state) {
     return { loginPage: state.loginPage }
 }

export default connect(mapStateToProps)(LoginPage);

login_form is boolean, and I intentionaly wrote PropType.string to see if it was working but it didn't give me any errors, this is probably because of redux connect,but I couldn't search anything about that. please anyone tell me what I am doing wrong :/ thanks.

解决方案

From the docs:

When an invalid value is provided for a prop, a warning will be shown in the JavaScript console. For performance reasons, propTypes is only checked in development mode.

If you're running the project that is not in development mode, then you cannot see the warning.

See update below: Also, PropTypes doesn't throw an error but show warning. Be sure to check warning in the console. You might have selected to show error only.

And also, be sure to import the PropTypes from 'prop-types' to work with PropTypes:

import PropTypes from 'prop-types'


If the above thing is assured and you still don't see the warning in the console, then there's one probability you pass boolean value in string:

<LoginPage login_form="true" />

Or,

<LoginPage login_form={'true'} />

Be sure to pass boolean value like this:

<LoginPage login_form={true} />

Note: if you want to pass the true value, you may just pass the props like this:

<LoginPage login_form />

Now, having login_form: PropTypes.string will show you warning.


Update:

Though react doc says it will throw warning, I just verified that it actually throws an error without application hold. But the message is starting with Warning:. Thus, be sure to check error in the console not warning.

Or, you may be sure to check default.

这篇关于道具类型在反应应用程序中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆