When writing JSX in React, adhere to these fundamental syntax rules:
- No quotes for Virtual DOM: Do not wrap JSX tags in quotes.
- JS Expressions: Wrap JavaScript expressions in curly braces
{}. - CSS Classes: Use
className instead of class to specify CSS classes. - Inline Styles: Inline styles must be provided as objects.
- Single Root Element: A Virtual DOM tree must have exactly one root element.
- Closing Tags: All tags must be closed (e.g.,
<br />). - Tag Casing:
- Lowercase: React treats tags starting with lowercase letters as standard HTML elements. If no matching HTML element exists, it throws an error.
- Uppercase: React treats tags starting with uppercase letters as custom components. If the component is not defined, it throws an error.
function App() {
const text = 'Hello, React!';
return (
<div className='App'>
<span style={{ fontSize: '24px' }}>{text}</span>
<br />
<p>This is a lowercase tag (HTML)</p>
</div>
);
}