GithubHelp home page GithubHelp logo

Comments (8)

RafalFilipek avatar RafalFilipek commented on June 21, 2024 4

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.

acdlite avatar acdlite commented on June 21, 2024

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.

jimbolla avatar jimbolla commented on June 21, 2024

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.

timmolendijk avatar timmolendijk commented on June 21, 2024

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

timmolendijk avatar timmolendijk commented on June 21, 2024

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.

wuct avatar wuct commented on June 21, 2024

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.

wuct avatar wuct commented on June 21, 2024

However, it seems like there is still no consensus whether recompose should provide lifecycle hooks. See #124.

from recompose.

acdlite avatar acdlite commented on June 21, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.