Skip to content
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

Bringing ReasonReact reducers to React.js #10596

Closed
mxstbr opened this issue Sep 2, 2017 · 3 comments
Closed

Bringing ReasonReact reducers to React.js #10596

mxstbr opened this issue Sep 2, 2017 · 3 comments

Comments

@mxstbr
Copy link
Contributor

mxstbr commented Sep 2, 2017

It'd be awesome if React.js had reducers, similar to what ReasonReact recently introduced.

We’re also decided to iterate on ReasonReact instead of core React, partially because we’re Reason users and partially because ReasonReact’s nature of being new & less used allow us to iterate exciting new APIs without legacy concerns

Since I couldn't find any previous talk about this I'm assuming this has been discussed internally, it'd be great to chat about it in the open and iterate on core React state management!

The ReasonReact reducers example
type actions =
  | BumpCount
  | ToggleDialog bool;

type state = {count: int, showDialog: bool};

let component = ReasonReact.reducerComponent "MyComponent";

let make ::className _children => {
  ...component,
  initialState: fun () => {count: 0, showDialog: false},
  reducer: fun action state =>
    switch action {
    | BumpCount => ReasonReact.Update {...state, count: state.count + 1}
    | ToggleDialog show => ReasonReact.Update {...state, showDialog: show}
    },
  render: fun {state, reduce} => {
    let message = "You've clicked " ^ string_of_int state.count ^ " time(s)";
    <div className>
      <button onClick=(reduce (fun _ => BumpCount))>
        (ReasonReact.stringToElement message)
      </button>
      <MyButton onToggle=(reduce (fun toggleState => ToggleDialog toggleState)) />
      (state.showDialog ? <Dialog /> : ReasonReact.nullElement)
    </div>
  }
};
@alexeyraspopov
Copy link
Contributor

@acdlite acdlite closed this as completed Sep 5, 2017
@acdlite
Copy link
Collaborator

acdlite commented Sep 5, 2017

Let's move discussion of new component APIs into the React Future repo.

it'd be great to chat about it in the open and iterate on core React state management

For more context: we're holding off on any major additions or changes to the component API until async rendering is more stable. There are many new constraints and quirks created by async rendering. It would be counterproductive for us to introduce new APIs now, only to find out later that those APIs aren't resilient to async behaviors.

@trueadm is also experimenting with a React component compiler, which will likely also influence the design of the new API.

tl;dr: Change is coming, but not right now.

(And yes, we're in constant collaboration with the Reason team, including in the design of the new reducer API.)

@acdlite
Copy link
Collaborator

acdlite commented Sep 5, 2017

Related: #10580 and #10581

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants