What does JSX compile to, and how does that explain these rules?It compiles to calls like React.createElement or jsx(). A function returns one value, and class is a reserved word in JavaScript.
What renders if you write {count && <List />} and count is 0?It renders 0. React skips false, null, undefined and true, but it renders numbers. Use count > 0 or a ternary instead.
Is it safe to put user input inside JSX braces?React escapes strings before it inserts them, which blocks most XSS. dangerouslySetInnerHTML skips that escaping.
What you can say
JSX looks like HTML, but it compiles to JavaScript function calls, and almost every rule comes from that.
A component has to return one parent element, so if I have siblings I wrap them in a div or a Fragment.
Attributes are camelCase, so class becomes className, since class is a reserved word in JavaScript, and onclick becomes onClick.
Every tag has to close, and a tag with no children closes itself with a slash, like <img />.
Curly braces switch into JavaScript, so {count} or any other expression works, and a boolean prop like disabled can be written bare to mean true.
The catch is that braces take expressions, not statements, so an if or a for loop has to become a ternary, && or map, or move above the return.
Weak answers to avoid
Recites the rules with no reasonA list of className and /> sounds memorized. Tie each rule to JSX compiling to JavaScript, for example class being a reserved word.
Calls JSX HTML inside JavaScriptJSX is syntax that compiles to function calls. Calling it HTML hides the reason its rules differ from HTML, which is what the interviewer wants to hear.
Says any JavaScript can go in bracesBraces take expressions only. An if or for statement there is a syntax error, so use a ternary or &&, or compute the value before return.
Adds a wrapper div for every sibling groupExtra divs clutter the DOM and can break table or flex layouts. Name Fragment as the way to return siblings without an extra node.
Interview lens
Likely follow-ups, what you can say, and the weak answers to avoid.