Reputation: 163
I want to solve this warning. I get this warning once I added react-router-dom to App.js. (The application itself is working fine with this warning.)
Warning message:
index.js:1 Warning: Failed prop type: Invalid prop 'component' supplied to 'Route': the prop is not a valid React component
in Route (at App.js:34)
in App (at src/index.js:9)
in Router (created by HashRouter)
in HashRouter (at src/index.js:8)
I'm just passing down the state down to the child component form App.js App.js
import React, { Component } from 'react'
import { BrowserRouter as Router, Switch, Route } from 'react-router-dom'
import './App.css'
import MasterPassword from './MasterPassword'
import EncryptForm from './EncryptForm'
import NotFound from './NotFound'
class App extends Component {
constructor(props) {
super(props)
this.state = {
masterPassword: null
}
}
getMasterPassword = userPassword => {
this.setState({
masterPassword: userPassword
})
}
render() {
return (
<Router>
{!this.state.masterPassword
? <MasterPassword
path='/ask-password'
masterPassword={this.state.masterPassword}
onStorePassword={this.getMasterPassword}
/>
: <div className="App">
<Switch>
<Route exact path='/' render={() => <EncryptForm masterPassword={this.state.masterPassword} />} />
<Route component={<NotFound />} />
</Switch>
</div>}
</Router>
)
}
}
export default App
index.js
import React from 'react'
import ReactDOM from 'react-dom'
import { HashRouter } from 'react-router-dom'
import './index.css'
import App from './components/App'
import * as serviceWorker from './serviceWorker'
ReactDOM.render(<HashRouter>
<App />
</HashRouter>, document.getElementById('root'))
serviceWorker.unregister()
Thank you!
Upvotes: 3
Views: 6668
Reputation: 729
@Asher's answer should work, but if you have props that you'd like to pass in, then you can pass the component in an inline function that creates the React element:
<Switch><Route component={() => <NotFound message={notFoundMessage} />}/></Switch>
Resource: https://ui.dev/react-router-v4-pass-props-to-components/
Upvotes: 1
Reputation: 269
Change this <Route component={<NotFound />} />
to this: <Route component={NotFound} />
This is pretty standard behavior for libraries like this. They want to render the component like this: <component />
rather than like this: {component}
.
Upvotes: 4