I want to pass string from Main to Header. It succeeds but warning. I'm a beginner of React so I can not figure out what it must be a function
means.
Anyone knows how to solve this warning?
The warning is:
And my code is below:
Main.js
import React from 'react'; import Header from './Header'; import AppList from './AppList/AppList'; import Footer from './Footer'; const propTypes = { mainInfo: React.PropTypes.shape({ title: React.PropTypes.string.isRequired, apps: React.PropTypes.array.isRequired, }), }; class Main extends React.Component { static methodsAreOk() { return true; } render() { return ( <div> <Header title={this.props.mainInfo.title} /> <AppList apps={this.props.mainInfo.apps} /> <Footer /> </div> ); } } Main.propTypes = propTypes; export default Main;
Header.js
import React from 'react'; const propTypes = { title: React.PropTypes.string.isRequred, }; class Header extends React.Component { static methodsAreOk() { return true; } render() { return ( <div className="header"> <h1>{this.props.title}</h1> </div> ); } } Header.propTypes = propTypes; export default Header;
PropTypes exports a range of validators that can be used to make sure the data you receive is valid. In this example, we're using PropTypes.string . 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.
PropTypes are simply a mechanism that ensures that the passed value is of the correct datatype. This makes sure that we don't receive an error at the very end of our app by the console which might not be easy to deal with.
PropTypes. node: any render-able value like numbers and string, that can actually be rendered on screen. PropTypes. any: any type of value, even those are non-render-able like boolean.
Typescript and PropTypes serve different purposes. Typescript validates types at compile time, whereas PropTypes are checked at runtime. Typescript is useful when you are writing code: it will warn you if you pass an argument of the wrong type to your React components, give you autocomplete for function calls, etc.
You have an error: React.PropTypes.string.isRequred
. Spell isRequired
correctly, and it should be ok.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With