Warning: Failed propType: Invalid prop `component` supplied to `Route`

后端 未结 11 816
礼貌的吻别
礼貌的吻别 2020-12-29 01:09

I\'m trying new react-router 1.0.0 and I\'m getting strange warnings I can\'t explain:

Warning: Failed propType: Invalid prop `component` supplied to `Ro

相关标签:
11条回答
  • 2020-12-29 01:20

    If you are not giving export default then it throws an error. check if you have given module.exports = Speaker; //spelling mistake here you have written exoprts and check in all the modules whether you have exported correct.

    0 讨论(0)
  • 2020-12-29 01:20

    In my case i leave my .js file empty means i never write anything in my .js file after that i was using it in my route so make function component or class component and finally export it will work

    0 讨论(0)
  • 2020-12-29 01:23

    Standardize your module's imports and exports then you won't risk hitting problems with misspelled property names.

    module.exports = Component should become export default Component.

    CommonJS uses module.exports as a convention, however, this means that you are just working with a regular Javascript object and you are able to set the value of any key you want (whether that's exports, exoprts or exprots). There are no runtime or compile-time checks to tell you that you've messed up.

    If you use ES6 (ES2015) syntax instead, then you are working with syntax and keywords. If you accidentally type exoprt default Component then it will give you a compile error to let you know.

    In your case, you can simplify the Speaker component.

    import React from 'react';
    
    export default React.createClass({
      render() {
        return (
          <h1>Speaker</h1>
        )
      }
    });
    
    0 讨论(0)
  • 2020-12-29 01:26

    It's a syntax issue related to imports/exports in your files, mine resolved by removing an extra quote from my import

    <Route path={`${match.path}/iso-line-number`} component={ISOLineNumber} />
    
    0 讨论(0)
  • 2020-12-29 01:27

    it is solved in react-router-dom 4.4.0 see: Route's proptypes fail

    now it is beta, or just wait for final release.

    npm install react-router-dom@4.4.0-beta.6 --save
    
    0 讨论(0)
  • 2020-12-29 01:30

    react-router@3.2.3 also fixed this bug, just update it:

    npm i --save react-router@latest
    
    0 讨论(0)
提交回复
热议问题