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?










share|improve this question
























  • 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















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?










share|improve this question
























  • 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













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?










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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


















  • 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












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;





share|improve this answer























  • 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










  • 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












  • I will use one of these... thank you
    – phantomCoder
    2 days ago











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















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
































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;





share|improve this answer























  • 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










  • 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












  • I will use one of these... thank you
    – phantomCoder
    2 days ago















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;





share|improve this answer























  • 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










  • 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












  • I will use one of these... thank you
    – phantomCoder
    2 days ago













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;





share|improve this answer














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;






share|improve this answer














share|improve this answer



share|improve this answer








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 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










  • 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












  • 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










  • 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


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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




















































































Popular posts from this blog

Bressuire

Vorschmack

Quarantine