-
Notifications
You must be signed in to change notification settings - Fork 3
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
Conversation
* 'master' of https://github.com/dagda1/react: initialise state in constructor
There was a problem hiding this 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.
@taras happy to make any changes or be challenged on anything and thanks for the feedback. |
This will cause an extra render cycle initiated from the constructor which you generally want to avoid. There is talk of deprecating |
It's all good. We can merge and I'll release. Thank you for your first contribution :) |
The state is initialised in the constructor now:
I'd like to have one
type
props and change its proptypes toPropTypes.func.isRequired
and remove the falsy check in the above.I've also moved the subscription of the observable to
componentDidMount
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 withthis.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.