Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

it must be a function, usually from React.PropTypes

Tags:

reactjs

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:

enter image description here

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; 
like image 792
morizotter Avatar asked Mar 21 '16 09:03

morizotter


People also ask

What is the PropTypes for function?

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.

What are the PropTypes in React?

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.

What does PropTypes node mean?

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.

What is PropTypes in typescript?

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.


1 Answers

You have an error: React.PropTypes.string.isRequred. Spell isRequired correctly, and it should be ok.

like image 184
dannyjolie Avatar answered Sep 18 '22 17:09

dannyjolie