Based on a conversation with @vjeux:
I wrote
constructor(props) { return { something: false } }
instead of
constructor(props) { super(props); this.state = { something: false } }
and the error was super confusing, it told me that render is not defined on the component, which didn't make sense since I returned it!
We can fix this to show a different message if type
has .prototype.isReactComponent
but constructor()
gave us something without a render
method.
hi I would like to work on it. Could you please point me to specific direction. I would really appreciate it.
I think the relevant code is in ReactFiberClassComponent. I would suggest adding a test first (e.g. try to reproduce the existing warning), then try to make changes for a better warning. Does this help?
Hey, @M-ZubairAhmed, I thought this was a good starting bug and went ahead with the fix as an exercise for myself. If you don't ultimately take this bug, let me know so I can create a pull request.
@M-ZubairAhmed Have you been able to look at it yet?
@deanbrophy yes even I have started working on it but since I am a beginner is taking time for me to complete. But I think it's not wise for me to hold it up, you are welcome to continue.
@gaearon I will keep looking out the space for more issues in future
@M-ZubairAhmed Thanks! The next "good first issue" is yours.
Maybe it is not related. Basically all the components that we are writing in Class style must deal with the state. Why cannot we enforce returning an initial state in Class style instead of doing this.state = {}
Because returning something from a constructor already has a (different) meaning in JavaScript.
If you're just looking for a terse syntax, use class properties and don't define a constructor at all.
Most helpful comment
I think the relevant code is in ReactFiberClassComponent. I would suggest adding a test first (e.g. try to reproduce the existing warning), then try to make changes for a better warning. Does this help?