Comments (8)
Lifecycle events are cool but I think there is no reason for packages like recompose-lifecycle
in that case. Quick example:
import React from 'react'
import { render } from 'react-dom'
import { compose, lifecycle, branch, renderNothing, withState, renderComponent } from 'recompose'
// returns HOC
const componentWithApi = (BaseComponent, requests) => (
compose(
// are we done?
withState('loaded', 'setLoaded', false),
lifecycle(
(c) => (
// Lets make all requests and then update state
Promise.all(requests.map(r => r()))
.then(() => c.props.setLoaded(true))
),
() => {}
),
// You can show some loader while data are loading or
// render nothig.
branch(
({ loaded }) => loaded,
// BOOOM! Ready to go!
renderComponent(BaseComponent),
// loading ... loading .. loading
renderComponent(() => <div>loader</div>),
)
)(renderNothing())
)
// Your complex application
const App = ({ loaded }) => <div>App { loaded ? 'yes' : 'no' }</div>
App.propTypes = { loaded: React.PropTypes.bool }
// Array of requests (set timeout mock)
const requests = [
() => new Promise((resolve) => setTimeout(resolve, 1000)),
() => new Promise((resolve) => setTimeout(resolve, 2000))
]
// You can wrap any component with componentWithApi function
const AppWithApi = componentWithApi(App, requests)
render(<AppWithApi />, document.querySelector('#app'))
from recompose.
Thanks for the suggestion! I'm not sure to what extent Recompose should be providing lifecycle hooks. I'd like to think about this for a while before making any decisions.
from recompose.
Hmmm. Well, one option would be to have a separate package recompose-lifecycle
for stuff like that, if there's enough code to justify one. Anyways, here's my naive implementation of onMount:
const onMount = (getData, BaseComponent) => React.createClass({
contextTypes: BaseComponent.contextTypes,
getInitialState() {
return {};
},
componentDidMount() {
getData(this.props, this.context)
.then(promise => {
if (this.isMounted)
this.setState({promise});
});
},
render() {
return (
<BaseComponent {...this.props} {...this.state.promise} />
);
},
});
from recompose.
@RafalFilipek This breaks in universal app situations (server-side kick-off alongside client-side run-time).
The missing aspect here is that a lifecycle method such as componentDidMount
is essential to some (many) data loading scenarios in universal apps, because it is invoked on client but not on server. As long as recompose doesn't offer some way of making this distinction, it won't enable me to get rid of the class notation.
from recompose.
Btw, I'm currently using the following home-grown doOnMount
function:
import { Component } from 'react';
import createHelper from 'recompose/createHelper';
import createElement from 'recompose/createElement';
export const doOnMount = createHelper(callback => BaseComponent =>
class extends Component {
componentDidMount() {
callback(this.props);
}
render() {
return createElement(BaseComponent, this.props);
}
}
, 'doOnMount');
Let's find out how it suits my purpose…
from recompose.
I am agree with @timmolendijk. componentDidMount
is pretty useful and it's the reason why react-lifecycle-hoc
exists. Currently, the only implementation in react-lifecycle-hoc
is componentDidMount
.
from recompose.
However, it seems like there is still no consensus whether recompose
should provide lifecycle hooks. See #124.
from recompose.
Closing because we now have two ways to do this: rx-recompose's mapPropsStream
(which will soon be part of Recompose itself) and the lifecycle
helper. Any other proposals should be brought up in a separate issue/PR. Thanks!
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.