Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[React.memo] Warning: Failed prop type: Invalid prop component of type object supplied to Route, expected function #6471

Closed
Albert-Gao opened this issue Nov 13, 2018 · 29 comments

Comments

@Albert-Gao
Copy link

Albert-Gao commented Nov 13, 2018

  "dependencies": {
    "react": "^16.7.0-alpha1",
    "react-dom": "^16.7.0-alpha1",
    "react-router-dom": "^4.3.1",
    "react-scripts": "2.1.1"
  },

How to reproduce:

  1. create-react-app try
  2. edit the App.js to the following:
import React, {memo} from "react";

import { BrowserRouter, Route } from "react-router-dom";

const Test = memo(() => <div>Quick Test</div>);

function App() {
  return (
    <BrowserRouter>
      <Route path="*" component={Test} />
    </BrowserRouter>
  );
}

export default App;
  1. yarn start, rendering works fine!

  2. check the console:

index.js:1452 Warning: Failed prop type: Invalid prop component of type object supplied to Route, expected function.
in Route (at App.js:10)
in App (at src/index.js:7)

I fired this issue at mobxjs/mobx-react-lite#12 as well.

@timdorr
Copy link
Member

timdorr commented Nov 13, 2018

This is fixed in 4.4. You can use the beta or wait for a final release.

@danielkcz
Copy link

danielkcz commented Nov 13, 2018

@timdorr I am curious. How is it actually fixed? I mean the prop types validator is still using react-is underneath which is being patched in this fresh new PR created a couple hours ago. I am either missing something here or you might have misunderstood the issue at hand? :)

@vensa-albertgao
Copy link

vensa-albertgao commented Nov 14, 2018

OK, now this is more interesting. I just migrated our company code from using react-loadable to React.lazy. But react-router gives the same error that I reported yesterday in the issue.

And I indeed updated "react-router-dom" to "^4.4.0-beta.6",

Nothing changes... Same error.

Did I miss something here?

I assume the procedure would be, wait the PR for react-is to be landed, then we update the react-router afterward.

@vensa-albertgao
Copy link

vensa-albertgao commented Nov 14, 2018

Found the PR for React-router for bumping the version number, after landing, the issue shall resolved, #6447

@danielkcz
Copy link

@vensa-albertgao I wouldn't be so sure about that. There is still pending PR for react-is for handling React.memo. So if the PR #6447 gets merged now, it won't solve this particular issue for sure.

@Albert-Gao
Copy link
Author

Albert-Gao commented Nov 14, 2018

@FredyC haha, yes, exactly. Forgot that, yes, even this PR lands, we need the actual fix land on react-is. But wait, how could this react router PR knows which version to bump... If react-is has not been fixed, the final version number is not there, yet, right...?

@frehner
Copy link
Contributor

frehner commented Nov 19, 2018

From what I can tell, the bump in #6447 will fix this issue for memo.

The PR linked to by @FredyC is for prop-types and not for react-is; the latest react-is already supports validating memo as noted here in the source code.

At least, from what I'm seeing. I could be wrong, but that's my interpretation of things.

@alan345
Copy link

alan345 commented Dec 7, 2018

same here.
image

I have removed node_module as advised #6447 (comment) but no changes

@frehner
Copy link
Contributor

frehner commented Dec 7, 2018

#6447 was merged but hasn't been released yet, so you'll still get that error until there's a new release

@seifsg
Copy link

seifsg commented Jan 26, 2019

@frehner Hi, when will be there the new release?

@frehner
Copy link
Contributor

frehner commented Jan 26, 2019

@seifsg not sure, I’m not someone who can make a new one so you’ll have to ask those in charge

@pixelypants
Copy link

Keen to get a new release also, how can we help?

@VincentLanglet
Copy link

No new release since four months.
What is missing @mjackson ? Can we help ?

@muralikrishnana
Copy link

muralikrishnana commented Mar 2, 2019

Try using <Route render ={()=> < Test />} path="path" /> rather than using the component prop.

@stack-wuh
Copy link

image
i resolved it

@The-Code-Monkey
Copy link

@stack-wuh what have you actually done to resolve it a screenshot isnt really much help

@vadymrybak
Copy link

vadymrybak commented Mar 13, 2019

Not sure if it's relevant. But in my case I'm using react hooks with router. So in order to get rid of this message I used this code:

import React, { useEffect, lazy } from 'react'
import { Switch, Route, match, withRouter } from 'react-router';

const OrderRoute = () => {

const Orders = lazy(() => import('../Orders/Orders'));
const Order = lazy(() => import('../Order/Order'));

useEffect(() => {
    console.log("%cOrderRoute component loaded / updated", 'color: blue');
});

return <React.Fragment>
         <Switch>
            <Route exact path='/orders' component={withRouter(Orders)}/>
            <Route path='/orders/:orderId' component={withRouter(Order)}/>
        </Switch>
    </React.Fragment>

}

export default OrderRoute;

basically adding withRouter removes the message

@PutziSan
Copy link

basically adding withRouter removes the message

Good idea, but you should not use a HOC inside the render function.

I can confirm that this workaround works for 4.3:

import React, { lazy, Suspense } from "react";
import { Route, withRouter } from "react-router-dom";

const LazyComponent = withRouter(lazy(() => import("./YourComponent")));

export function App() {
  return (
    <Suspense fallback={<div>loading...</div>}>
      <Route component={LazyComponent} />
    </Suspense>
  );
}

@danielkcz
Copy link

danielkcz commented Mar 14, 2019

@PutziSan It feels fairly silly to wrap component into another HOC just because DEV-only prop types check is failing :)

What about trying 4.4-beta instead? I had no problem with that so far.

@luisnoresv
Copy link

luisnoresv commented May 24, 2019

This issue was fixed on the last version of react-router-dom@5.0.0

@agustin107
Copy link

agustin107 commented Jun 11, 2019

I've resolved updating react-router-dom@5.0.1 and react-router@5.0.1.

@gpbl
Copy link

gpbl commented Jul 15, 2019

We still have this issue and we can't upgrade to v5 because it breaks our production build. Is there an alternative to fix this warning?

@sag1v
Copy link

sag1v commented Jul 23, 2019

As @gpbl mentioned, some of us can't upgrade to v4 / v5.
So i wonder what is the perf penalty for:

instead of

<Route path='mypath' component={MyComponent} />

to do this

<Route path='mypath' component={props => <MyComponent {...props} />} />

@mqklin
Copy link

mqklin commented Jul 23, 2019

@sag1v you should use render={props=> <MyComponent {...props}/>} , not component={props => <MyComponent {...props}/>}

@sag1v
Copy link

sag1v commented Jul 23, 2019

Why not component?
Are you sure render exists on all versions? (3.2.0 for example)

@mqklin
Copy link

mqklin commented Jul 23, 2019

Hmm, not sure about 3.2.0.
I think component={props => ...} will cause big perf problems, because it will rerender your route component every time.

@sag1v
Copy link

sag1v commented Jul 23, 2019

because it will rerender your route
component every time

Everytime what, a route change?

@mqklin
Copy link

mqklin commented Jul 24, 2019

Every time your parent component (where you render <Route...) rerenders. I'm saying this because I had the same problem, that I replaced component with render and now it works OK.

@guilhermecatalan
Copy link

Sounds silly to say but... in some cases this is not a bug. One of your routes may be pointing to an empty file, throwing this error.

@lock lock bot locked as resolved and limited conversation to collaborators Sep 27, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests