Comments (13)
I've been thinking lately about removing lifecycle
all together. The lifecycle methods are essentially useless without access to the component instance, in which case you should just use a class.
from recompose.
Does this make sense to anyone else? Is lifecycle
really all that useful?
from recompose.
Here is simple way to intercept livecycle method calls:
import { compose, toClass } from 'recompose';
const enhance = compose(
component => class extends toClass(component) {
componentWillReceiveProps(nextProps) {
if (nextProps.value !== this.props.value) {
// Do something, value has changed.
}
}
},
);
Simply extend the given component in the compose chain, and do whatever you need there.
from recompose.
+1 for this issue.
It looks inconsistent for now. I.e., we already dealing with DOM lifecycle hooks, in willUnmount
- so why there is no other DOM hooks?
from recompose.
The lifecycle methods are essentially useless without access to the component instance
Well, not exactly. For example, you can start some timers when component is mounted - note, not in constructor, as it will cause problems with server-side rendering. Or, my own case, with which I came to this issue - a print page. It looks like this:
class PrintPage extends React.Component {
render() {
return (
<Content {...this.props} />
);
}
componentDidMount() {
window.print();
}
}
With didMount
hook it could be just lifecycle(window.print, Content)
.
Sure, it's all a very edge cases. But they exist, and it's always nicely when there is a handy function which saves you from manually writing another boilerplate class =)
from recompose.
Have a question about:
What the idea to pass this
as an argument to lifecycle methods, isn't it better to pass just props and some internal object to hold data across lifecycle events (like window event handlers or some instance methods which depends on lifecycle).
To get state access user can use lifecycle
in combination with withState
.
Does something like this will not be enough?
class Lifecycle extends React.Component {
internalObj_ = null
componentWillMount() {
this.internalObj_ = setup(props)
}
...otherLifeCycleEventsIfNeeded
componentWillUnmount() {
teardown(this.props, this.internalObj_)
}
render() {
return createElement(BaseComponent, {...this.props, ...this.internalObj_})
}
}
from recompose.
Without this
, props could be changed at level above, so setup
internals will have a ref on an old props version
from recompose.
My use case is just to load an SDK(braintree) during componentWillMount
only on client side for later use. I don't need access to the wrapped component instance.
from recompose.
I've been using createSink
or doOnReceiveProps
when I need to deal with componentWillMount
. Wouldn't that do for those use cases?
The downside is that it triggers on componentWillReceiveProps
and sometimes you don't want that, e.g.: you may just want to trigger something once off. So perhaps something can be done there too (either once off components or a switch on those two).
What are your thoughts on this?
from recompose.
@dariocravero I use lifecycle
for things like initial caches as well, where they aren't really depending on the props at all. Just a setState most of the time
from recompose.
Useful:
- api calls
- timeouts
- intervals
from recompose.
voted for keeping doOnReceiveProps
in another thread.
since it removed. at least provide a way to do something on Mount/NewProps since that helper was very handy really.
agree with @RafalFilipek
from recompose.
Closed by #162.
from recompose.
Related Issues (20)
- renderNothing should export Nothing HOT 4
- Does TypeScript 3.4 enable typing of compose HOT 1
- Remove flow types in repo and support only in flow-typed repo HOT 1
- Key advantages in Docs HOT 2
- The best way to test a component that use getContext recompose HOT 1
- Get a lifecycle componentDidMount unmounted error HOT 1
- Rewriting mapPropsStream with Hooks or new Lifecycle Methods HOT 4
- compose hoc causes to lose typings
- Remove usage of `createFactory` HOT 4
- Why does `mapProps` remove `children`? HOT 2
- node-fetch vulnerability issue (denial of service) HOT 8
- Forked, Updated, and Published HOT 1
- fbjs dependency still present in lock file HOT 3
- Support React 17 HOT 2
- Suggest alternative to branch(), renderComponent(), renderNothing(), compose(), etc
- TypeError: Cannot read properties of undefined (reading 'apply')
- Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.
- New patch required to mitigate high risk vulnerability
- Maintainers HOT 1
- This is GITHUB and not the European Parliament or NATO.
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from recompose.