react-redux-typescript-guide icon indicating copy to clipboard operation
react-redux-typescript-guide copied to clipboard

suggestion: recompose examples

Open arolson101 opened this issue 7 years ago • 11 comments

I found this document to be very helpful! Thank you for putting it together.

It might be useful to add some typescript examples of recompose

arolson101 avatar Jan 19 '18 20:01 arolson101

That's an interesting suggestion, unfortunately I'm not using recompose on daily basis but this is something I was interested in learning at some point. btw. contributions are welcomed :)

piotrwitek avatar Jan 20 '18 20:01 piotrwitek

@arolson101 why do you think recompose examples belongs here? I believe it's completely different topic

pablobirukov avatar Feb 06 '18 09:02 pablobirukov

@r00ger for example, the withState example is a recompose component. They're just useful for making higher order components.

arolson101 avatar Feb 06 '18 22:02 arolson101

In my case I have been using something like this:

component.tsx

import React from 'react';
import { compose } from 'recompose';
import { withTranslations, WithTranslationsProps } from './i18n';
import { toggable, ToggableHandlers, ToggableState } from './toggable';

export type ComponentOwnProps = {
    className?: string;
    disabled?: boolean;
    bar: string;
}
export type ComponentProps = ComponentOwnProps
    & WithTranslationsProps
    & ToggableHandlers
    & ToggableState;


export const Component = compose<ComponentProps, ComponentOwnProps>(
    withTranslations,
    toggable,
)((props: ComponentProps) => {
    const { className, disabled, toggle, isOpen, i18n } = props;
    return (
        <div className={className} disabled={disabled}>
            <button onClick={toggle}>{isOpen ? i18n('Close') : i18n('Open')}</button>
        </div>
    );
});

toggable.tsx

export type ToggableState = typeof initState;
export type ToggableHandlers = {
    toggle: () => ToggableState;
    setIsOpen: (isOpen: boolean) => ToggableState;
};

const initState = {
    isOpen: false,
};

const handlers = {
    toggle: (state: ToggableState) => () => ({ isOpen: !state.isOpen }),
    setIsOpen: () => (isOpen: boolean) => ({ isOpen }),
};

export const toggable = Recompose.withStateHandlers<
    ToggableState,
    ToggableHandlers
>(initState, handlers);

Hope it helps

IOuser avatar Aug 22 '18 15:08 IOuser

@boostio funded this issue with $10. Visit this issue on Issuehunt

IssueHuntBot avatar Sep 14 '18 09:09 IssueHuntBot

@loadbalance-sudachi-kun funded this issue with $256. Visit this issue on Issuehunt

IssueHuntBot avatar Sep 17 '18 03:09 IssueHuntBot

@piotrwitek has started working. Visit this issue on Issuehunt

IssueHuntBot avatar Sep 17 '18 08:09 IssueHuntBot

@piotrwitek has submitted output. Visit this issue on Issuehunt

IssueHuntBot avatar Sep 17 '18 11:09 IssueHuntBot

Planned work:

  • add a new recompose section with setup instructions
  • add usage code examples of the most common recompose API in the playground project
  • add autogeneration of docs based on code examples based on their implementation
  • include code examples in the build pipeline (format, lint, typecheck etc.)

piotrwitek avatar Sep 17 '18 11:09 piotrwitek

https://github.com/acdlite/recompose

A Note from the Author (acdlite, Oct 25 2018): Hi! I created Recompose about three years ago. About a year after that, I joined the React team. Today, we announced a proposal for Hooks. Hooks solves all the problems I attempted to address with Recompose three years ago, and more on top of that. I will be discontinuing active maintenance of this package (excluding perhaps bugfixes or patches for compatibility with future React releases), and recommending that people use Hooks instead. Your existing code with Recompose will still work, just don't expect any new features. Thank you so, so much to @wuct and @istarkov for their heroic work maintaining Recompose over the last few years.

muescha avatar Apr 06 '19 23:04 muescha

I think we should move to hooks instead

sibelius avatar Mar 12 '20 11:03 sibelius