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

Make reducer an instance method instead of a static method #21

Open
nataly87s opened this issue Oct 12, 2018 · 2 comments
Open

Make reducer an instance method instead of a static method #21

nataly87s opened this issue Oct 12, 2018 · 2 comments

Comments

@nataly87s
Copy link
Contributor

nataly87s commented Oct 12, 2018

  • the class can be abstract
  • it will be more type-safe because you can't pass generic types to static methods in typescript.
  • you won't have to pass this to the side-effect

Alternatively - you can pass the reducer function to the constructor, to make sure that the reducer doesn't have access to the class instance. and this is also more type-safe

@philipp-spiess
Copy link
Owner

My initial approach used instance methods for the reducer. You can find some comments about it in #3

I agree that passing of this as the first argument to the side effect is a a bit ugly but that's also what ReactReason ended up doing: https://github.com/reasonml/reason-react/blob/master/docs/state-actions-reducer.md#state-update-through-reducer

Alternatively - you can pass the reducer function to the constructor, to make sure that the reducer doesn't have access to the class instance. and this is also more type-safe

I'm not quite sure I understand. When you use React, you don't control what is passed to the constructor - React is calling it for you.

@nataly87s
Copy link
Contributor Author

nataly87s commented Oct 13, 2018

The ReComponent class is a super class (like react's Component), so the component that extends ReComponent can pass an additional argument to the super constructor

class MyComponent extends ReComponent {
  constructor(props) {
    super(reducer, props);
  } 
} 

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

2 participants