Better way of extending React components2019 Community Moderator ElectionLoop inside React JSXreact-router - pass props to handler componentCan you force a React component to rerender without calling setState?What is the difference between using constructor vs getInitialState in React / React Native?What do these three dots in React do?Programmatically navigate using react routerHow to conditionally add attributes to React components?Detect click outside React componentWhat is the difference between React Native and React?React-Router Link, how to trigger a click event on a Link from another component
Create chunks from an array
Is every open circuit a capacitor?
Calculate total length of edges in select Voronoi diagram
When to use the term transposed instead of modulation?
Error in TransformedField
Sundering Titan and basic normal lands and snow lands
The need of reserving one's ability in job interviews
Linear Combination of Atomic Orbitals
How to make sure I'm assertive enough in contact with subordinates?
Rationale to prefer local variables over instance variables?
Can a space-faring robot still function over a billion years?
Replacing tantalum capacitor with ceramic capacitor for Op Amps
Named nets not connected in Eagle board design
How do you make a gun that shoots melee weapons and/or swords?
Split a number into equal parts given the number of parts
Ultrafilters as a double dual
Why would the IRS ask for birth certificates or even audit a small tax return?
Is divide-by-zero a security vulnerability?
Called into a meeting and told we are being made redundant (laid off) and "not to share outside". Can I tell my partner?
Do natural melee weapons (from racial traits) trigger Improved Divine Smite?
3.5% Interest Student Loan or use all of my savings on Tuition?
How can friction do no work in case of pure rolling?
Computing the volume of a simplex-like object with constraints
Why is there an extra space when I type "ls" on the Desktop?
Better way of extending React components
2019 Community Moderator ElectionLoop inside React JSXreact-router - pass props to handler componentCan you force a React component to rerender without calling setState?What is the difference between using constructor vs getInitialState in React / React Native?What do these three dots in React do?Programmatically navigate using react routerHow to conditionally add attributes to React components?Detect click outside React componentWhat is the difference between React Native and React?React-Router Link, how to trigger a click event on a Link from another component
I have a none UI related plugin in vanilla javascript called SignalR.EventAggregatorProxy. Its a server - client pub/sub library.
I already have a Vue plugin and now look at implementing a React plugin. I need some lifetime knowledge of the component. One way is extending React.Component and let the users of the lib use this component instead of the orignal.
class MyEvent
constructor(message)
this.message = message;
class SignalREventAggregatorComponent extends React.Component
subscribe(event, handler, constraint)
setTimeout(() => handler(new MyEvent("This comes from backend")), 1000); //signalR.eventAggregator.subscribe(event, handler, this, constraint)
publish(event)
//signalR.eventAggregator.publish(event);
componentWillUnmount()
//signalR.eventAggregator.unsubscribe(this);
class TestComponent extends SignalREventAggregatorComponent
componentDidMount()
this.subscribe(MyEvent, this.onEvent);
componentWillUnmount()
//Doing some teardown in TestComponent
super.componentWillUnmount(); // This is paramount to call
onEvent(e)
console.log(e);
render()
return (
<div>
</div>
);
This is a bit fragile, first its not nice to force the users to extend a specific component like this. They might have another third party library they already extend etc. Second its fragile. The user needs to call super.componentWillUnmount otherwise my library will fail to unsubscribe the component and server side events will keep streaming in. (Plus it will be a memory leak). Is there a nicer way?
I need to know when the component dies, plus I must know its context (this keyword basically). Also the component need a nice way of calling methods on the plugin to subscribe and publish events.
Any ideas?
reactjs react-component
add a comment |
I have a none UI related plugin in vanilla javascript called SignalR.EventAggregatorProxy. Its a server - client pub/sub library.
I already have a Vue plugin and now look at implementing a React plugin. I need some lifetime knowledge of the component. One way is extending React.Component and let the users of the lib use this component instead of the orignal.
class MyEvent
constructor(message)
this.message = message;
class SignalREventAggregatorComponent extends React.Component
subscribe(event, handler, constraint)
setTimeout(() => handler(new MyEvent("This comes from backend")), 1000); //signalR.eventAggregator.subscribe(event, handler, this, constraint)
publish(event)
//signalR.eventAggregator.publish(event);
componentWillUnmount()
//signalR.eventAggregator.unsubscribe(this);
class TestComponent extends SignalREventAggregatorComponent
componentDidMount()
this.subscribe(MyEvent, this.onEvent);
componentWillUnmount()
//Doing some teardown in TestComponent
super.componentWillUnmount(); // This is paramount to call
onEvent(e)
console.log(e);
render()
return (
<div>
</div>
);
This is a bit fragile, first its not nice to force the users to extend a specific component like this. They might have another third party library they already extend etc. Second its fragile. The user needs to call super.componentWillUnmount otherwise my library will fail to unsubscribe the component and server side events will keep streaming in. (Plus it will be a memory leak). Is there a nicer way?
I need to know when the component dies, plus I must know its context (this keyword basically). Also the component need a nice way of calling methods on the plugin to subscribe and publish events.
Any ideas?
reactjs react-component
can't it be a react hook?
– CD..
yesterday
Did you consider creatingSignalREventAggregatorComponentas an HOC? reactjs.org/docs/higher-order-components.html It may suit your need.. Though, the users will still need to use the HOC, so write something likeSignalREventAggregatorComponent(ComponentCreatedByUser), but you wouldn't have problem with extends nor the needing to callsuper.componentWillUnmount
– Matteo Gesmundo
yesterday
If typescript is a possibility for you, see here github.com/Microsoft/TypeScript/issues/21388
– Murat Karagöz
yesterday
add a comment |
I have a none UI related plugin in vanilla javascript called SignalR.EventAggregatorProxy. Its a server - client pub/sub library.
I already have a Vue plugin and now look at implementing a React plugin. I need some lifetime knowledge of the component. One way is extending React.Component and let the users of the lib use this component instead of the orignal.
class MyEvent
constructor(message)
this.message = message;
class SignalREventAggregatorComponent extends React.Component
subscribe(event, handler, constraint)
setTimeout(() => handler(new MyEvent("This comes from backend")), 1000); //signalR.eventAggregator.subscribe(event, handler, this, constraint)
publish(event)
//signalR.eventAggregator.publish(event);
componentWillUnmount()
//signalR.eventAggregator.unsubscribe(this);
class TestComponent extends SignalREventAggregatorComponent
componentDidMount()
this.subscribe(MyEvent, this.onEvent);
componentWillUnmount()
//Doing some teardown in TestComponent
super.componentWillUnmount(); // This is paramount to call
onEvent(e)
console.log(e);
render()
return (
<div>
</div>
);
This is a bit fragile, first its not nice to force the users to extend a specific component like this. They might have another third party library they already extend etc. Second its fragile. The user needs to call super.componentWillUnmount otherwise my library will fail to unsubscribe the component and server side events will keep streaming in. (Plus it will be a memory leak). Is there a nicer way?
I need to know when the component dies, plus I must know its context (this keyword basically). Also the component need a nice way of calling methods on the plugin to subscribe and publish events.
Any ideas?
reactjs react-component
I have a none UI related plugin in vanilla javascript called SignalR.EventAggregatorProxy. Its a server - client pub/sub library.
I already have a Vue plugin and now look at implementing a React plugin. I need some lifetime knowledge of the component. One way is extending React.Component and let the users of the lib use this component instead of the orignal.
class MyEvent
constructor(message)
this.message = message;
class SignalREventAggregatorComponent extends React.Component
subscribe(event, handler, constraint)
setTimeout(() => handler(new MyEvent("This comes from backend")), 1000); //signalR.eventAggregator.subscribe(event, handler, this, constraint)
publish(event)
//signalR.eventAggregator.publish(event);
componentWillUnmount()
//signalR.eventAggregator.unsubscribe(this);
class TestComponent extends SignalREventAggregatorComponent
componentDidMount()
this.subscribe(MyEvent, this.onEvent);
componentWillUnmount()
//Doing some teardown in TestComponent
super.componentWillUnmount(); // This is paramount to call
onEvent(e)
console.log(e);
render()
return (
<div>
</div>
);
This is a bit fragile, first its not nice to force the users to extend a specific component like this. They might have another third party library they already extend etc. Second its fragile. The user needs to call super.componentWillUnmount otherwise my library will fail to unsubscribe the component and server side events will keep streaming in. (Plus it will be a memory leak). Is there a nicer way?
I need to know when the component dies, plus I must know its context (this keyword basically). Also the component need a nice way of calling methods on the plugin to subscribe and publish events.
Any ideas?
reactjs react-component
reactjs react-component
edited yesterday
Anders
asked yesterday
AndersAnders
12.7k853120
12.7k853120
can't it be a react hook?
– CD..
yesterday
Did you consider creatingSignalREventAggregatorComponentas an HOC? reactjs.org/docs/higher-order-components.html It may suit your need.. Though, the users will still need to use the HOC, so write something likeSignalREventAggregatorComponent(ComponentCreatedByUser), but you wouldn't have problem with extends nor the needing to callsuper.componentWillUnmount
– Matteo Gesmundo
yesterday
If typescript is a possibility for you, see here github.com/Microsoft/TypeScript/issues/21388
– Murat Karagöz
yesterday
add a comment |
can't it be a react hook?
– CD..
yesterday
Did you consider creatingSignalREventAggregatorComponentas an HOC? reactjs.org/docs/higher-order-components.html It may suit your need.. Though, the users will still need to use the HOC, so write something likeSignalREventAggregatorComponent(ComponentCreatedByUser), but you wouldn't have problem with extends nor the needing to callsuper.componentWillUnmount
– Matteo Gesmundo
yesterday
If typescript is a possibility for you, see here github.com/Microsoft/TypeScript/issues/21388
– Murat Karagöz
yesterday
can't it be a react hook?
– CD..
yesterday
can't it be a react hook?
– CD..
yesterday
Did you consider creating
SignalREventAggregatorComponent as an HOC? reactjs.org/docs/higher-order-components.html It may suit your need.. Though, the users will still need to use the HOC, so write something like SignalREventAggregatorComponent(ComponentCreatedByUser), but you wouldn't have problem with extends nor the needing to call super.componentWillUnmount– Matteo Gesmundo
yesterday
Did you consider creating
SignalREventAggregatorComponent as an HOC? reactjs.org/docs/higher-order-components.html It may suit your need.. Though, the users will still need to use the HOC, so write something like SignalREventAggregatorComponent(ComponentCreatedByUser), but you wouldn't have problem with extends nor the needing to call super.componentWillUnmount– Matteo Gesmundo
yesterday
If typescript is a possibility for you, see here github.com/Microsoft/TypeScript/issues/21388
– Murat Karagöz
yesterday
If typescript is a possibility for you, see here github.com/Microsoft/TypeScript/issues/21388
– Murat Karagöz
yesterday
add a comment |
2 Answers
2
active
oldest
votes
Instead of inheritance, you can create a HOC and use it with your TestComponent!
Check out this example.
Thanks for a working example. Would you say HOC is more common for this than a custom hook?
– Anders
yesterday
Yes! Though if you have several components, that should share the same handler, you may want to use a Provider.
– Lorenz Henk
yesterday
1
Nah, that defeats the purpose of a event aggregator :P Thanks man, will test and accept
– Anders
yesterday
Glad I was able to help :)
– Lorenz Henk
yesterday
One final piece of the puzzel is missing. From the subscribe I need to get the context of the wrapped component. the this keyword does not point to same context
– Anders
yesterday
|
show 2 more comments
You can use some kind of a provider that the user will wrap his root component with, similar to what react-router or redux are doing. And also provide a wrapper component that will do the subscription.
Here's an example:
Root component rendering, your provider adds the subscribe function to the
application context. This is optional, depending on if you want to hold a unique state in your app:
render()
<YourProvider>
<App/>
</YourProvider>
An example for a component that want's to listen to events:
componentDidMount()
this.props.subscribe(MyEvent, this.onEvent);
In order for the component to get the subscribe in the props, the user can wrap his component with your wrapper component like this:
<SubscribeProvider>
<MyComponent/>
</SubscribeProvider>
or use a withSubscribe function that you also implement.
Is this the same as higher order components? (HOC)
– Anders
yesterday
Yeap, I've posted an example on how to implement this HOC, also added an optional provider to the root (If needed)
– gilamran
yesterday
add a comment |
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',
autoActivateHeartbeat: false,
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
);
);
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
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55023019%2fbetter-way-of-extending-react-components%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Instead of inheritance, you can create a HOC and use it with your TestComponent!
Check out this example.
Thanks for a working example. Would you say HOC is more common for this than a custom hook?
– Anders
yesterday
Yes! Though if you have several components, that should share the same handler, you may want to use a Provider.
– Lorenz Henk
yesterday
1
Nah, that defeats the purpose of a event aggregator :P Thanks man, will test and accept
– Anders
yesterday
Glad I was able to help :)
– Lorenz Henk
yesterday
One final piece of the puzzel is missing. From the subscribe I need to get the context of the wrapped component. the this keyword does not point to same context
– Anders
yesterday
|
show 2 more comments
Instead of inheritance, you can create a HOC and use it with your TestComponent!
Check out this example.
Thanks for a working example. Would you say HOC is more common for this than a custom hook?
– Anders
yesterday
Yes! Though if you have several components, that should share the same handler, you may want to use a Provider.
– Lorenz Henk
yesterday
1
Nah, that defeats the purpose of a event aggregator :P Thanks man, will test and accept
– Anders
yesterday
Glad I was able to help :)
– Lorenz Henk
yesterday
One final piece of the puzzel is missing. From the subscribe I need to get the context of the wrapped component. the this keyword does not point to same context
– Anders
yesterday
|
show 2 more comments
Instead of inheritance, you can create a HOC and use it with your TestComponent!
Check out this example.
Instead of inheritance, you can create a HOC and use it with your TestComponent!
Check out this example.
answered yesterday
Lorenz HenkLorenz Henk
1975
1975
Thanks for a working example. Would you say HOC is more common for this than a custom hook?
– Anders
yesterday
Yes! Though if you have several components, that should share the same handler, you may want to use a Provider.
– Lorenz Henk
yesterday
1
Nah, that defeats the purpose of a event aggregator :P Thanks man, will test and accept
– Anders
yesterday
Glad I was able to help :)
– Lorenz Henk
yesterday
One final piece of the puzzel is missing. From the subscribe I need to get the context of the wrapped component. the this keyword does not point to same context
– Anders
yesterday
|
show 2 more comments
Thanks for a working example. Would you say HOC is more common for this than a custom hook?
– Anders
yesterday
Yes! Though if you have several components, that should share the same handler, you may want to use a Provider.
– Lorenz Henk
yesterday
1
Nah, that defeats the purpose of a event aggregator :P Thanks man, will test and accept
– Anders
yesterday
Glad I was able to help :)
– Lorenz Henk
yesterday
One final piece of the puzzel is missing. From the subscribe I need to get the context of the wrapped component. the this keyword does not point to same context
– Anders
yesterday
Thanks for a working example. Would you say HOC is more common for this than a custom hook?
– Anders
yesterday
Thanks for a working example. Would you say HOC is more common for this than a custom hook?
– Anders
yesterday
Yes! Though if you have several components, that should share the same handler, you may want to use a Provider.
– Lorenz Henk
yesterday
Yes! Though if you have several components, that should share the same handler, you may want to use a Provider.
– Lorenz Henk
yesterday
1
1
Nah, that defeats the purpose of a event aggregator :P Thanks man, will test and accept
– Anders
yesterday
Nah, that defeats the purpose of a event aggregator :P Thanks man, will test and accept
– Anders
yesterday
Glad I was able to help :)
– Lorenz Henk
yesterday
Glad I was able to help :)
– Lorenz Henk
yesterday
One final piece of the puzzel is missing. From the subscribe I need to get the context of the wrapped component. the this keyword does not point to same context
– Anders
yesterday
One final piece of the puzzel is missing. From the subscribe I need to get the context of the wrapped component. the this keyword does not point to same context
– Anders
yesterday
|
show 2 more comments
You can use some kind of a provider that the user will wrap his root component with, similar to what react-router or redux are doing. And also provide a wrapper component that will do the subscription.
Here's an example:
Root component rendering, your provider adds the subscribe function to the
application context. This is optional, depending on if you want to hold a unique state in your app:
render()
<YourProvider>
<App/>
</YourProvider>
An example for a component that want's to listen to events:
componentDidMount()
this.props.subscribe(MyEvent, this.onEvent);
In order for the component to get the subscribe in the props, the user can wrap his component with your wrapper component like this:
<SubscribeProvider>
<MyComponent/>
</SubscribeProvider>
or use a withSubscribe function that you also implement.
Is this the same as higher order components? (HOC)
– Anders
yesterday
Yeap, I've posted an example on how to implement this HOC, also added an optional provider to the root (If needed)
– gilamran
yesterday
add a comment |
You can use some kind of a provider that the user will wrap his root component with, similar to what react-router or redux are doing. And also provide a wrapper component that will do the subscription.
Here's an example:
Root component rendering, your provider adds the subscribe function to the
application context. This is optional, depending on if you want to hold a unique state in your app:
render()
<YourProvider>
<App/>
</YourProvider>
An example for a component that want's to listen to events:
componentDidMount()
this.props.subscribe(MyEvent, this.onEvent);
In order for the component to get the subscribe in the props, the user can wrap his component with your wrapper component like this:
<SubscribeProvider>
<MyComponent/>
</SubscribeProvider>
or use a withSubscribe function that you also implement.
Is this the same as higher order components? (HOC)
– Anders
yesterday
Yeap, I've posted an example on how to implement this HOC, also added an optional provider to the root (If needed)
– gilamran
yesterday
add a comment |
You can use some kind of a provider that the user will wrap his root component with, similar to what react-router or redux are doing. And also provide a wrapper component that will do the subscription.
Here's an example:
Root component rendering, your provider adds the subscribe function to the
application context. This is optional, depending on if you want to hold a unique state in your app:
render()
<YourProvider>
<App/>
</YourProvider>
An example for a component that want's to listen to events:
componentDidMount()
this.props.subscribe(MyEvent, this.onEvent);
In order for the component to get the subscribe in the props, the user can wrap his component with your wrapper component like this:
<SubscribeProvider>
<MyComponent/>
</SubscribeProvider>
or use a withSubscribe function that you also implement.
You can use some kind of a provider that the user will wrap his root component with, similar to what react-router or redux are doing. And also provide a wrapper component that will do the subscription.
Here's an example:
Root component rendering, your provider adds the subscribe function to the
application context. This is optional, depending on if you want to hold a unique state in your app:
render()
<YourProvider>
<App/>
</YourProvider>
An example for a component that want's to listen to events:
componentDidMount()
this.props.subscribe(MyEvent, this.onEvent);
In order for the component to get the subscribe in the props, the user can wrap his component with your wrapper component like this:
<SubscribeProvider>
<MyComponent/>
</SubscribeProvider>
or use a withSubscribe function that you also implement.
answered yesterday
gilamrangilamran
3,36541941
3,36541941
Is this the same as higher order components? (HOC)
– Anders
yesterday
Yeap, I've posted an example on how to implement this HOC, also added an optional provider to the root (If needed)
– gilamran
yesterday
add a comment |
Is this the same as higher order components? (HOC)
– Anders
yesterday
Yeap, I've posted an example on how to implement this HOC, also added an optional provider to the root (If needed)
– gilamran
yesterday
Is this the same as higher order components? (HOC)
– Anders
yesterday
Is this the same as higher order components? (HOC)
– Anders
yesterday
Yeap, I've posted an example on how to implement this HOC, also added an optional provider to the root (If needed)
– gilamran
yesterday
Yeap, I've posted an example on how to implement this HOC, also added an optional provider to the root (If needed)
– gilamran
yesterday
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55023019%2fbetter-way-of-extending-react-components%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
can't it be a react hook?
– CD..
yesterday
Did you consider creating
SignalREventAggregatorComponentas an HOC? reactjs.org/docs/higher-order-components.html It may suit your need.. Though, the users will still need to use the HOC, so write something likeSignalREventAggregatorComponent(ComponentCreatedByUser), but you wouldn't have problem with extends nor the needing to callsuper.componentWillUnmount– Matteo Gesmundo
yesterday
If typescript is a possibility for you, see here github.com/Microsoft/TypeScript/issues/21388
– Murat Karagöz
yesterday