What’s the best way to deal with undefined props in react.js?

Posted by Jennifer | Updated on

What’s the best way to deal with undefined props in react.js?

Generally, the approach I like to take is to define default props on the component which have the semantic meaning of “empty.” For example, in context of the issue you describe I would typically structure my component like this (ES6 classes style):

This is relatively clean, prevents the code from throwing at run time, and forces you to create well-defined behaviors for semantically null, empty, or undefined input states.


If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.

Related Questions & Answers