New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A less-verbose / more-composable alternative: recompose #69

Closed
danscan opened this Issue Jan 10, 2018 · 1 comment

Comments

Projects
None yet
2 participants
@danscan

danscan commented Jan 10, 2018

Hey! This is a neat project to make state + function components convenient, but this could be a bit easier and more powerful via HOCs like recompose provides.

Reducer example

import React from 'react';
import ReactDOM from 'react-dom';
import { withReducer } from 'recompose';

// Actions
const ADD = 'ADD';
const SUBTRACT = 'SUBTRACT';

// Action creators
const add = () => ({ type: ADD });
const subtract = () => ({ type: SUBTRACT });

const enhance = withReducer(
  stateName: 'counter',
  dispatchName: 'reduce',
  reducer: (state, action) => {
    switch (action.type) {
      case ADD:
        return state - 1;
      case SUBTRACT:
        return state + 1;
      default:
        return state;
    }
  },
  initialState: 0,
);

const Counter = enhance(({
  counter,
  reduce,
}) => (
  <div>
      <button onClick={() => reduce(add())}>+</button>
      <span>{counter}</span>
      <button onClick={() => reduce(subtract())}>-</button>
  </div>
));

ReactDOM.render(
  <Counter />,
  document.getElementById('app'),
);

Simpler example

import React from 'react';
import ReactDOM from 'react-dom';
import { withStateHandlers } from 'recompose';

const enhance = withStateHandlers(
  { counter: 0 },
  {
    add: ({ counter }) => () => ({ counter: counter + 1 }),
    subtract: ({ counter }) => () => ({ counter: counter - 1 }),
  },
);

const Counter = enhance(({
  add,
  counter
  subtract,
}) => (
  <div>
      <button onClick={add}>+</button>
      <span>{counter}</span>
      <button onClick={subtract}>-</button>
  </div>
));

ReactDOM.render(
  <Counter />,
  document.getElementById('app'),
);

One nice thing is that benefit of composition: you could make enhance in these examples a module that you can use on any component expecting the props it passes down.

I wanted to create this issue because I figured you might be interested in this pattern. Not trying to talk smack on this project at all :)

@vejersele

This comment has been minimized.

Show comment
Hide comment
@vejersele

vejersele Jan 11, 2018

Owner

Hey! Thank you for taking the time to create this issue, I definitely appreciate it!

I am familiar with recompose and actually use it quite often. I believe there's definitely an overlap between what you can do with React Stateful Component and recompose, but there's also a couple of differences. For example, in React Stateful Component reducers can also schedule side-effects. There's also the subscriptions, etc ...

The goal of this library is to provide a similar experience as you would get with Elm and ReasonML, but in javascript, with flow types.

I definitely agree with you that it sometimes gets a little verbose, but currently I don't see that as a major issue. I'm going to keep this in the back of my mind while working on this project, there might be some stuff that can be simplified in the future.

Thanks,

Jelle

Owner

vejersele commented Jan 11, 2018

Hey! Thank you for taking the time to create this issue, I definitely appreciate it!

I am familiar with recompose and actually use it quite often. I believe there's definitely an overlap between what you can do with React Stateful Component and recompose, but there's also a couple of differences. For example, in React Stateful Component reducers can also schedule side-effects. There's also the subscriptions, etc ...

The goal of this library is to provide a similar experience as you would get with Elm and ReasonML, but in javascript, with flow types.

I definitely agree with you that it sometimes gets a little verbose, but currently I don't see that as a major issue. I'm going to keep this in the back of my mind while working on this project, there might be some stuff that can be simplified in the future.

Thanks,

Jelle

@vejersele vejersele closed this Jan 11, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment