GithubHelp home page GithubHelp logo

Comments (4)

hnordt avatar hnordt commented on July 20, 2024

Here is data fetching example I imagine:

import React from 'react';
import ReactDOM from 'react-dom';
import { compose, withState, mapProps } from 'recompose';

const Counter = ({ counter, getCount }) => (
  <p>
    {counter.isLoading ? 'Loading...' : (counter.data ? `Count: ${counter.data}` : null)}
    <button onClick={getCount}>Get Count</button>
  </p>
);

// Simulate backend
let count = 0;
const getCount = callback => {
  count++;
  setTimeout(() => callback(count), 1000);
};

const CounterContainer = compose(
  withState('counter', 'updateCounter', {}),
  mapProps(({ updateCounter, ...rest }) => ({
    getCount: () => {
      updateCounter({ isLoading: true });
      getCount(count => updateCounter({ data: count }));
    },
    ...rest
  }))
)(Counter);

ReactDOM.render(<CounterContainer />, document.getElementById('root'));

from recompose.

acdlite avatar acdlite commented on July 20, 2024

Usually when you connect a component to an external data source, you need a subscription that is disposed when the component unmounts. Check out the lifecycle() helper

EDIT: Oops, linked to source instead of API docs

from recompose.

hnordt avatar hnordt commented on July 20, 2024

@acdlite awesome.

So data fetching inside container components with Recompose is acceptable? I mean, it's scalable?

from recompose.

acdlite avatar acdlite commented on July 20, 2024

I can't say for certain since I haven't tried it yet, but using lifecycle it shouldn't be any different than using constructor() and componentWillUnmount() directly, which is how data fetching in normal React classes usually works.

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.