React : Warning: Failed prop type: Cannot read property 'apply' of undefined

Shashaank

I am working on a navbar in react and i have been getting this error, and i have not found a working solution yet. Here is the piece of my code. I am trying to use react prop-types library to validate my navbar props with either a link or a dropdown. Below is the piece of code i have written.

NavBar.js

const NavBar = ({ navItems }) => {
return (
    <nav role="navigation" className={styles.navBar}>
        <Logo type="text">
            ABCD
        </Logo>
        <div className={[styles.collapse, styles.noCollapse].join(' ')}>
            <SearchBox />
            <NavItems navItemsData={navItems} />
        </div>
    </nav>
);
};

NavItems.js

const NavItems = ({ navItemsData }) => {
return (
    <ul className={styles.navItems}>
        {navItemsData.map(navItemData => {
            let navItem = <NavItem {...navItemData} key={navItemData.id} />
            if (navItemData.type === 'dropdown') {
                navItem = <NavDropDownItem {...navItemData} key={navItemData.id} />
            }
            return navItem;
        })}
    </ul>
);
};

PropTypes Checker(in same file as NavItems) :-

NavItems.propTypes = {
navItemsData: PropTypes.arrayOf(PropTypes.shape({
    id : PropTypes.number,
    type: PropTypes.oneOf(['link', 'dropdown']).isRequired,
    linkText: requiredIf(PropTypes.string.isRequired, props => props.type === 'link'),
    title : requiredIf(PropTypes.string.isRequired, props => props.type === 'dropdown'),
    dropDownList: requiredIf(PropTypes.arrayOf(PropTypes.shape({ linkText: PropTypes.string.isRequired })), props => props.type === 'dropdown')
}))
};

I keep getting this warning in the console. As follows :-

Warning: Failed prop type: Cannot read property 'apply' of undefined
    in NavItems (at NavBar.js:15)
    in NavBar (at App.js:35)
    in div (at App.js:34)
    in App (at src/index.js:7)

The props i am passing :

const navItems = [{
    id : 1,
    type : 'link',
    linkText : 'Link1'
  },{
    id : 2,
    type : 'link',
    linkText : 'Link2'
  }, {
    id : 3,
    type : 'link',
    linkText : 'Link3'
  }, {
    id : 4,
    type : 'link',
    linkText : 'Link4'
  },{
    id : 5,
    type : 'link',
    linkText : 'Link5'
  },{
    id : 6,
    type : 'dropdown',
    dropDownList : [{
      linkText : 'LinkText'
    }]
  }]

Any help would be appreciated.

Udith Gunaratna

When you are using requiredIf, the first parameter should be the expected type. But it should not have the isRequired part. For example, your validation should be as follows.

NavItems.propTypes = {
    navItemsData: PropTypes.arrayOf(PropTypes.shape({
      id : PropTypes.number,
      type: PropTypes.oneOf(['link', 'dropdown']).isRequired,
      linkText: requiredIf(PropTypes.string, props => props.type === 'link'),
      title : requiredIf(PropTypes.string, props => props.type === 'dropdown'),
      dropDownList: requiredIf(PropTypes.arrayOf(PropTypes.shape({ linkText: PropTypes.string.isRequired })), props => props.type === 'dropdown')
}))};

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Pass function as a prop in react, Cannot read property '_handleEvent' of undefined

Passing a function as a prop in react, Cannot read property 'edit' of undefined

Warning: failed prop type in React error

React Warning: "Failed prop type: checker is not a function"

React build failed: cannot read property 'name' of undefined

React Warning: Failed prop type: Invalid prop of type `Object` supplied

Uncaught TypeError: Cannot read property 'prop' of undefined

FIREBASE WARNING: Cannot read property 'myID' of undefined

React - Cannot read property of undefined

Cannot read property '' of undefined (react)?

React cannot read property of undefined

Uncaught TypeError: Cannot read property 'type' of undefined React/Redux

Cannot read property 'type' of undefined (react-router-redux)

Cannot read property 'type' of undefined - react-redux

Type Error: Cannot read the property of the undefined - React Js Web Api

Redux and React-Naitve: Cannot read property 'type' of undefined

React work from Redux . TypeError: Cannot read property 'type' of undefined

TypeError: Cannot read property 'apply' of undefined at EventEmitter

Redux TypeError: Cannot read property 'apply' of undefined

Cannot read property 'apply' of undefined - Youtube Api

"TypeError: Cannot read property 'apply' of undefined"

MongoSkin "Cannot read property 'apply' of undefined"

Failed: Cannot read property 'getWebElements' of undefined

Failed: Cannot read property 'isDisplayed' of undefined

React is undefined (Cannot read property 'createElement' of undefined)

Cannot read property [PROPERTY] of undefined in React native

Warning: Failed propType: Invalid prop of type `array` expected `object` with React

Display list of trails in React - "Warning: Failed prop type"

Const component cannot read property of undefined even though it is a prop?

TOP Ranking

HotTag

Archive