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

Initialise state in constructor an refactor tests #1

Merged
merged 5 commits into from
Mar 7, 2018
Merged

Initialise state in constructor an refactor tests #1

merged 5 commits into from
Mar 7, 2018

Conversation

dagda1
Copy link

@dagda1 dagda1 commented Mar 7, 2018

The state is initialised in the constructor now:

  constructor(props = {}) {
    super(props);

    let { Type, type, value } = this.props;

    Type = Type || type;

    // TODO: this can go if we only have type and PropTypes.func.isRequired
    if (!Type) {
      let name = this.displayName || this.name || Microstates.name;
      console.error(`${name} expects Type prop to be specified but none was received.`);
      return;
    }

    let microstate = create(Type, value);

    this.state = { next: microstate };
  }

I'd like to have one type props and change its proptypes to PropTypes.func.isRequired and remove the falsy check in the above.

I've also moved the subscription of the observable to componentDidMount

  componentDidMount() {
    let observable = Observable.from(this.state.next);

    this.subscription = observable.subscribe(this.onNext);
  }

You want to avoid doing anything that can cause side effects to after the initial render.

Ideally I would not create the Microstate in the constructor and do it in componentDidMount and instead initialise in the constructor with this.state = { next: value }; but I'm not sure what the ramifications of this are.

I've also removed the stateful variable used to assert in the tests.

@taras taras self-requested a review March 7, 2018 18:58
Copy link
Member

@taras taras left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to have one type props and change its proptypes to PropTypes.func.isRequired and remove the falsy check in the above.

From what I understand, PropTypes only does warning. It won't prevent the component tree from attempting to render when Type is not specified. Not providing type is a developer error, the app should essentially stop rendering if Type is not present.

I've also moved the subscription of the observable to componentDidMount

I'm ok with this because the net difference is very minor but it's worth pointing out that doing setup in constructor is a very low cost operation because microstates are lazy. Very little work is done before the component is rendered and the render functions starts to pull state and transitions from the microstate. For this reason, it's fairly safe (IMO) to leave the subscription in the constructor. Although, I'm fine with this because technically it's correct.

Regarding un-subscription, I left it out previously because I don't think it's actually necessary because microstates are pure. They don't store anything or change anything outside of them. Once the component is garbage collected, the microstates and the subscription are garbage collected as well.

Unless React does some component instant pooling/reuse, I doubt there'll be any difference.

With all of that said, I'm good with these changes because they're technically correct on all accounts.

@dagda1
Copy link
Author

dagda1 commented Mar 7, 2018

@taras happy to make any changes or be challenged on anything and thanks for the feedback.

@dagda1
Copy link
Author

dagda1 commented Mar 7, 2018

it's fairly safe (IMO) to leave the subscription in the constructor.

This will cause an extra render cycle initiated from the constructor which you generally want to avoid. componentWillMount is discourage for this reason also and does not fire when server rendering.

There is talk of deprecating componentWillMount

@taras
Copy link
Member

taras commented Mar 7, 2018

It's all good. We can merge and I'll release. Thank you for your first contribution :)

@taras taras merged commit c3830cd into microstates:master Mar 7, 2018
@dagda1 dagda1 deleted the init-state branch March 7, 2018 23:16
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

Successfully merging this pull request may close these issues.

None yet

2 participants