Use hoist-non-react-statics with withRouter
up vote
4
down vote
favorite
How to use hoist-non-react-statics
with withRouter
I am adding a static method in Feedback
component.
This was my original code. I am trying to use new changes in Context API (react v 16.6)
Feedback.contextType = AppContext;
export default withRouter( Feedback );
This works fine, but I am getting the below warning in console.
Warning: withRouter(Feedback): Function components do not support
contextType.
So to fix the warning I used the method proposed by Dan here. Its also mentioned in react docs
So I have this code now which is not working.
Imported the hoist-non-react-statics
import {Link, withRouter} from 'react-router-dom';
import hoistNonReactStatics from 'hoist-non-react-statics';
And exported the component like this
Feedback.contextType = AppContext;
hoistNonReactStatics( Feedback, withRouter(Feedback) );
export default Feedback;
but for some reason router info (history, match etc) is not populated in props
Any pointers why its not working?
javascript reactjs router react-router-v4
add a comment |
up vote
4
down vote
favorite
How to use hoist-non-react-statics
with withRouter
I am adding a static method in Feedback
component.
This was my original code. I am trying to use new changes in Context API (react v 16.6)
Feedback.contextType = AppContext;
export default withRouter( Feedback );
This works fine, but I am getting the below warning in console.
Warning: withRouter(Feedback): Function components do not support
contextType.
So to fix the warning I used the method proposed by Dan here. Its also mentioned in react docs
So I have this code now which is not working.
Imported the hoist-non-react-statics
import {Link, withRouter} from 'react-router-dom';
import hoistNonReactStatics from 'hoist-non-react-statics';
And exported the component like this
Feedback.contextType = AppContext;
hoistNonReactStatics( Feedback, withRouter(Feedback) );
export default Feedback;
but for some reason router info (history, match etc) is not populated in props
Any pointers why its not working?
javascript reactjs router react-router-v4
It's unclear what the problem is. What's the connection between hoistNonReactStatics and props? Are you saying that props work ok without hoistNonReactStatics but don't work with hoistNonReactStatics ?
– estus
Nov 10 at 15:03
@estus I have updated the question with more details...
– phantomCoder
Nov 10 at 15:24
I couldn't reproduce this warning but provided a fix. Hope this helps.
– estus
Nov 10 at 17:03
add a comment |
up vote
4
down vote
favorite
up vote
4
down vote
favorite
How to use hoist-non-react-statics
with withRouter
I am adding a static method in Feedback
component.
This was my original code. I am trying to use new changes in Context API (react v 16.6)
Feedback.contextType = AppContext;
export default withRouter( Feedback );
This works fine, but I am getting the below warning in console.
Warning: withRouter(Feedback): Function components do not support
contextType.
So to fix the warning I used the method proposed by Dan here. Its also mentioned in react docs
So I have this code now which is not working.
Imported the hoist-non-react-statics
import {Link, withRouter} from 'react-router-dom';
import hoistNonReactStatics from 'hoist-non-react-statics';
And exported the component like this
Feedback.contextType = AppContext;
hoistNonReactStatics( Feedback, withRouter(Feedback) );
export default Feedback;
but for some reason router info (history, match etc) is not populated in props
Any pointers why its not working?
javascript reactjs router react-router-v4
How to use hoist-non-react-statics
with withRouter
I am adding a static method in Feedback
component.
This was my original code. I am trying to use new changes in Context API (react v 16.6)
Feedback.contextType = AppContext;
export default withRouter( Feedback );
This works fine, but I am getting the below warning in console.
Warning: withRouter(Feedback): Function components do not support
contextType.
So to fix the warning I used the method proposed by Dan here. Its also mentioned in react docs
So I have this code now which is not working.
Imported the hoist-non-react-statics
import {Link, withRouter} from 'react-router-dom';
import hoistNonReactStatics from 'hoist-non-react-statics';
And exported the component like this
Feedback.contextType = AppContext;
hoistNonReactStatics( Feedback, withRouter(Feedback) );
export default Feedback;
but for some reason router info (history, match etc) is not populated in props
Any pointers why its not working?
javascript reactjs router react-router-v4
javascript reactjs router react-router-v4
edited Nov 10 at 15:24
asked Nov 10 at 14:44
phantomCoder
231318
231318
It's unclear what the problem is. What's the connection between hoistNonReactStatics and props? Are you saying that props work ok without hoistNonReactStatics but don't work with hoistNonReactStatics ?
– estus
Nov 10 at 15:03
@estus I have updated the question with more details...
– phantomCoder
Nov 10 at 15:24
I couldn't reproduce this warning but provided a fix. Hope this helps.
– estus
Nov 10 at 17:03
add a comment |
It's unclear what the problem is. What's the connection between hoistNonReactStatics and props? Are you saying that props work ok without hoistNonReactStatics but don't work with hoistNonReactStatics ?
– estus
Nov 10 at 15:03
@estus I have updated the question with more details...
– phantomCoder
Nov 10 at 15:24
I couldn't reproduce this warning but provided a fix. Hope this helps.
– estus
Nov 10 at 17:03
It's unclear what the problem is. What's the connection between hoistNonReactStatics and props? Are you saying that props work ok without hoistNonReactStatics but don't work with hoistNonReactStatics ?
– estus
Nov 10 at 15:03
It's unclear what the problem is. What's the connection between hoistNonReactStatics and props? Are you saying that props work ok without hoistNonReactStatics but don't work with hoistNonReactStatics ?
– estus
Nov 10 at 15:03
@estus I have updated the question with more details...
– phantomCoder
Nov 10 at 15:24
@estus I have updated the question with more details...
– phantomCoder
Nov 10 at 15:24
I couldn't reproduce this warning but provided a fix. Hope this helps.
– estus
Nov 10 at 17:03
I couldn't reproduce this warning but provided a fix. Hope this helps.
– estus
Nov 10 at 17:03
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
The second snippet isn't supposed to work because withRouter(Feedback)
isn't exported from the module.
As linked issue explains, the problem was that hoist-non-react-statics
wasn't treated contextType
static property correctly. This has been fixed in latest hoist-non-react-statics
version. Since react-router
uses older hoist-non-react-statics
version as a dependency, this could be fixed in-place:
Feedback.contextType = AppContext;
export default Object.assign(withRouter(Feedback), { contextType: undefined });
Or:
Feedback.contextType = AppContext;
const FeedbackWithRouter = withRouter(Feedback);
delete FeedbackWithRouter.contextType;
export default FeedbackWithRouter;
Or:
export default withRouter(Feedback);
Feedback.contextType = AppContext;
contentType
is added in version 3.1.0 github.com/mridgway/hoist-non-react-statics/pull/62
– phantomCoder
Nov 10 at 17:53
That's correct. While react-router's dependency is v2. Please, provide a way to replicate the problem. As I mentioned, I wasn't able to replicate it. If the problem was thatwithRouter(Feedback)
inherited contextType from wrapped component then a fix would solve it.
– estus
Nov 10 at 17:55
Warning is gone, but this method has a disadvantage as mentioned in React docs, we need to know the static method in prior. I am looking for a solution withhoist-non-react-statics
– phantomCoder
Nov 10 at 17:56
It seems that you misunderstand the role of hoist-non-react-statics here. It's already applied by withRouter. Applying it again won't do any good. All you can do is to negate its effects. You don't necessarily need to know whether contextType is there or not. It should just go away in enhanced component. You can use your own helper,let withRouter(...args) => { let Comp = ReactRouter.withRouter(...args); delete Comp.contextType; return Comp }
Or fork react-router, update it hoist-non-react-statics@^3.1 and possibly do a PR.
– estus
Nov 10 at 18:01
I will use one of these... thank you
– phantomCoder
2 days ago
|
show 1 more comment
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
The second snippet isn't supposed to work because withRouter(Feedback)
isn't exported from the module.
As linked issue explains, the problem was that hoist-non-react-statics
wasn't treated contextType
static property correctly. This has been fixed in latest hoist-non-react-statics
version. Since react-router
uses older hoist-non-react-statics
version as a dependency, this could be fixed in-place:
Feedback.contextType = AppContext;
export default Object.assign(withRouter(Feedback), { contextType: undefined });
Or:
Feedback.contextType = AppContext;
const FeedbackWithRouter = withRouter(Feedback);
delete FeedbackWithRouter.contextType;
export default FeedbackWithRouter;
Or:
export default withRouter(Feedback);
Feedback.contextType = AppContext;
contentType
is added in version 3.1.0 github.com/mridgway/hoist-non-react-statics/pull/62
– phantomCoder
Nov 10 at 17:53
That's correct. While react-router's dependency is v2. Please, provide a way to replicate the problem. As I mentioned, I wasn't able to replicate it. If the problem was thatwithRouter(Feedback)
inherited contextType from wrapped component then a fix would solve it.
– estus
Nov 10 at 17:55
Warning is gone, but this method has a disadvantage as mentioned in React docs, we need to know the static method in prior. I am looking for a solution withhoist-non-react-statics
– phantomCoder
Nov 10 at 17:56
It seems that you misunderstand the role of hoist-non-react-statics here. It's already applied by withRouter. Applying it again won't do any good. All you can do is to negate its effects. You don't necessarily need to know whether contextType is there or not. It should just go away in enhanced component. You can use your own helper,let withRouter(...args) => { let Comp = ReactRouter.withRouter(...args); delete Comp.contextType; return Comp }
Or fork react-router, update it hoist-non-react-statics@^3.1 and possibly do a PR.
– estus
Nov 10 at 18:01
I will use one of these... thank you
– phantomCoder
2 days ago
|
show 1 more comment
up vote
1
down vote
accepted
The second snippet isn't supposed to work because withRouter(Feedback)
isn't exported from the module.
As linked issue explains, the problem was that hoist-non-react-statics
wasn't treated contextType
static property correctly. This has been fixed in latest hoist-non-react-statics
version. Since react-router
uses older hoist-non-react-statics
version as a dependency, this could be fixed in-place:
Feedback.contextType = AppContext;
export default Object.assign(withRouter(Feedback), { contextType: undefined });
Or:
Feedback.contextType = AppContext;
const FeedbackWithRouter = withRouter(Feedback);
delete FeedbackWithRouter.contextType;
export default FeedbackWithRouter;
Or:
export default withRouter(Feedback);
Feedback.contextType = AppContext;
contentType
is added in version 3.1.0 github.com/mridgway/hoist-non-react-statics/pull/62
– phantomCoder
Nov 10 at 17:53
That's correct. While react-router's dependency is v2. Please, provide a way to replicate the problem. As I mentioned, I wasn't able to replicate it. If the problem was thatwithRouter(Feedback)
inherited contextType from wrapped component then a fix would solve it.
– estus
Nov 10 at 17:55
Warning is gone, but this method has a disadvantage as mentioned in React docs, we need to know the static method in prior. I am looking for a solution withhoist-non-react-statics
– phantomCoder
Nov 10 at 17:56
It seems that you misunderstand the role of hoist-non-react-statics here. It's already applied by withRouter. Applying it again won't do any good. All you can do is to negate its effects. You don't necessarily need to know whether contextType is there or not. It should just go away in enhanced component. You can use your own helper,let withRouter(...args) => { let Comp = ReactRouter.withRouter(...args); delete Comp.contextType; return Comp }
Or fork react-router, update it hoist-non-react-statics@^3.1 and possibly do a PR.
– estus
Nov 10 at 18:01
I will use one of these... thank you
– phantomCoder
2 days ago
|
show 1 more comment
up vote
1
down vote
accepted
up vote
1
down vote
accepted
The second snippet isn't supposed to work because withRouter(Feedback)
isn't exported from the module.
As linked issue explains, the problem was that hoist-non-react-statics
wasn't treated contextType
static property correctly. This has been fixed in latest hoist-non-react-statics
version. Since react-router
uses older hoist-non-react-statics
version as a dependency, this could be fixed in-place:
Feedback.contextType = AppContext;
export default Object.assign(withRouter(Feedback), { contextType: undefined });
Or:
Feedback.contextType = AppContext;
const FeedbackWithRouter = withRouter(Feedback);
delete FeedbackWithRouter.contextType;
export default FeedbackWithRouter;
Or:
export default withRouter(Feedback);
Feedback.contextType = AppContext;
The second snippet isn't supposed to work because withRouter(Feedback)
isn't exported from the module.
As linked issue explains, the problem was that hoist-non-react-statics
wasn't treated contextType
static property correctly. This has been fixed in latest hoist-non-react-statics
version. Since react-router
uses older hoist-non-react-statics
version as a dependency, this could be fixed in-place:
Feedback.contextType = AppContext;
export default Object.assign(withRouter(Feedback), { contextType: undefined });
Or:
Feedback.contextType = AppContext;
const FeedbackWithRouter = withRouter(Feedback);
delete FeedbackWithRouter.contextType;
export default FeedbackWithRouter;
Or:
export default withRouter(Feedback);
Feedback.contextType = AppContext;
edited Nov 10 at 18:10
answered Nov 10 at 16:58
estus
62.4k2192199
62.4k2192199
contentType
is added in version 3.1.0 github.com/mridgway/hoist-non-react-statics/pull/62
– phantomCoder
Nov 10 at 17:53
That's correct. While react-router's dependency is v2. Please, provide a way to replicate the problem. As I mentioned, I wasn't able to replicate it. If the problem was thatwithRouter(Feedback)
inherited contextType from wrapped component then a fix would solve it.
– estus
Nov 10 at 17:55
Warning is gone, but this method has a disadvantage as mentioned in React docs, we need to know the static method in prior. I am looking for a solution withhoist-non-react-statics
– phantomCoder
Nov 10 at 17:56
It seems that you misunderstand the role of hoist-non-react-statics here. It's already applied by withRouter. Applying it again won't do any good. All you can do is to negate its effects. You don't necessarily need to know whether contextType is there or not. It should just go away in enhanced component. You can use your own helper,let withRouter(...args) => { let Comp = ReactRouter.withRouter(...args); delete Comp.contextType; return Comp }
Or fork react-router, update it hoist-non-react-statics@^3.1 and possibly do a PR.
– estus
Nov 10 at 18:01
I will use one of these... thank you
– phantomCoder
2 days ago
|
show 1 more comment
contentType
is added in version 3.1.0 github.com/mridgway/hoist-non-react-statics/pull/62
– phantomCoder
Nov 10 at 17:53
That's correct. While react-router's dependency is v2. Please, provide a way to replicate the problem. As I mentioned, I wasn't able to replicate it. If the problem was thatwithRouter(Feedback)
inherited contextType from wrapped component then a fix would solve it.
– estus
Nov 10 at 17:55
Warning is gone, but this method has a disadvantage as mentioned in React docs, we need to know the static method in prior. I am looking for a solution withhoist-non-react-statics
– phantomCoder
Nov 10 at 17:56
It seems that you misunderstand the role of hoist-non-react-statics here. It's already applied by withRouter. Applying it again won't do any good. All you can do is to negate its effects. You don't necessarily need to know whether contextType is there or not. It should just go away in enhanced component. You can use your own helper,let withRouter(...args) => { let Comp = ReactRouter.withRouter(...args); delete Comp.contextType; return Comp }
Or fork react-router, update it hoist-non-react-statics@^3.1 and possibly do a PR.
– estus
Nov 10 at 18:01
I will use one of these... thank you
– phantomCoder
2 days ago
contentType
is added in version 3.1.0 github.com/mridgway/hoist-non-react-statics/pull/62– phantomCoder
Nov 10 at 17:53
contentType
is added in version 3.1.0 github.com/mridgway/hoist-non-react-statics/pull/62– phantomCoder
Nov 10 at 17:53
That's correct. While react-router's dependency is v2. Please, provide a way to replicate the problem. As I mentioned, I wasn't able to replicate it. If the problem was that
withRouter(Feedback)
inherited contextType from wrapped component then a fix would solve it.– estus
Nov 10 at 17:55
That's correct. While react-router's dependency is v2. Please, provide a way to replicate the problem. As I mentioned, I wasn't able to replicate it. If the problem was that
withRouter(Feedback)
inherited contextType from wrapped component then a fix would solve it.– estus
Nov 10 at 17:55
Warning is gone, but this method has a disadvantage as mentioned in React docs, we need to know the static method in prior. I am looking for a solution with
hoist-non-react-statics
– phantomCoder
Nov 10 at 17:56
Warning is gone, but this method has a disadvantage as mentioned in React docs, we need to know the static method in prior. I am looking for a solution with
hoist-non-react-statics
– phantomCoder
Nov 10 at 17:56
It seems that you misunderstand the role of hoist-non-react-statics here. It's already applied by withRouter. Applying it again won't do any good. All you can do is to negate its effects. You don't necessarily need to know whether contextType is there or not. It should just go away in enhanced component. You can use your own helper,
let withRouter(...args) => { let Comp = ReactRouter.withRouter(...args); delete Comp.contextType; return Comp }
Or fork react-router, update it hoist-non-react-statics@^3.1 and possibly do a PR.– estus
Nov 10 at 18:01
It seems that you misunderstand the role of hoist-non-react-statics here. It's already applied by withRouter. Applying it again won't do any good. All you can do is to negate its effects. You don't necessarily need to know whether contextType is there or not. It should just go away in enhanced component. You can use your own helper,
let withRouter(...args) => { let Comp = ReactRouter.withRouter(...args); delete Comp.contextType; return Comp }
Or fork react-router, update it hoist-non-react-statics@^3.1 and possibly do a PR.– estus
Nov 10 at 18:01
I will use one of these... thank you
– phantomCoder
2 days ago
I will use one of these... thank you
– phantomCoder
2 days ago
|
show 1 more comment
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53240058%2fuse-hoist-non-react-statics-with-withrouter%23new-answer', 'question_page');
}
);
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
It's unclear what the problem is. What's the connection between hoistNonReactStatics and props? Are you saying that props work ok without hoistNonReactStatics but don't work with hoistNonReactStatics ?
– estus
Nov 10 at 15:03
@estus I have updated the question with more details...
– phantomCoder
Nov 10 at 15:24
I couldn't reproduce this warning but provided a fix. Hope this helps.
– estus
Nov 10 at 17:03