JSX in React Native
JSX in React Native Interview with follow-up questions
1. What is JSX and why is it used in React Native?
JSX is a syntax extension for JavaScript that lets you write UI markup directly in your code. It's not HTML — it's syntactic sugar that compiles to function calls (via the modern JSX transform, react/jsx-runtime) that create elements. In React Native, Hi compiles to jsx() calls, which the Fabric renderer turns into native views.
Why it's used: it keeps markup and logic together in one declarative, readable place, and because it's "just JavaScript," you can embed expressions, map over arrays, and compose components naturally.
Two gotchas interviewers like:
- In React Native, JSX tags are native components (
View,Text,Pressable), not HTML tags (div,span). Text must live inside a ``. - Since the React 17+ JSX transform, you don't need to
import Reactjust to use JSX — that older requirement is outdated.
Follow-up 1
Can you write React Native code without JSX?
Yes, it is possible to write React Native code without using JSX. Instead of using JSX, you can use the React.createElement function to create React Native elements. However, using JSX is the recommended approach as it provides a more concise and readable way to define components.
Follow-up 2
What are the benefits of using JSX?
There are several benefits of using JSX in React Native:
- Familiar syntax: JSX syntax is similar to HTML, making it easier for developers who are already familiar with web development.
- Component-based structure: JSX allows you to define reusable components with their own logic and styling.
- Code readability: JSX code is more readable and easier to understand compared to using plain JavaScript to create elements.
- Static type checking: JSX supports static type checking with tools like TypeScript or Flow, which helps catch errors early in the development process.
Follow-up 3
How does JSX differ from HTML?
While JSX syntax is similar to HTML, there are a few key differences:
- Class vs className: In JSX, you use the
classNameattribute instead of theclassattribute to define CSS classes. - Inline styles: In JSX, you define inline styles using JavaScript objects instead of using CSS syntax.
- Self-closing tags: In JSX, self-closing tags must be explicitly closed with a slash, like `
, whereas in HTML, some tags can be self-closed without the slash, like`.
- Attribute names: JSX attribute names use camelCase instead of kebab-case, so
classbecomesclassName, andforbecomeshtmlFor. - Expressions: JSX allows you to embed JavaScript expressions within curly braces
{}to dynamically generate content or attributes.
Follow-up 4
Can you give an example of a JSX expression in React Native?
Sure! Here's an example of a JSX expression in React Native:
import React from 'react';
import { View, Text } from 'react-native';
const App = () => {
return (
Hello, React Native!
);
};
export default App;
In this example, we define a functional component called App that renders a View component with some styling and a Text component with the text 'Hello, React Native!'. The styling is defined using inline styles with a JavaScript object.
2. How can you embed expressions in JSX?
You embed JavaScript expressions in JSX with curly braces {}. Anything that evaluates to a value works — variables, function calls, ternaries, array .map():
Hello, {user.name}! You have {count * 2} points.
The key gotcha interviewers probe: curly braces take an expression, not a statement. So if/for/switch aren't allowed inline — use a ternary or && for conditionals and .map() for lists:
{isLoggedIn ? Welcome : Sign in}
{items.map((item) => (
{item.label}
))}
And remember what JSX ignores: false, null, undefined, and true render nothing — handy for conditional UI ({error && {error}}), but watch the classic bug where a 0 from count && actually renders as text in a ``.
Follow-up 1
What is the syntax to embed expressions in JSX?
The syntax to embed expressions in JSX is to wrap the expression inside curly braces {}. For example, {2 + 2}.
Follow-up 2
Can you give an example of embedding an expression in JSX?
Sure! Here's an example of embedding an expression in JSX:
const name = 'John';
const element = <h1>Hello, {name}!</h1>;
Follow-up 3
What types of expressions can be embedded in JSX?
You can embed any valid JavaScript expression in JSX. This includes variables, function calls, arithmetic operations, conditional expressions, and more.
3. What are the limitations of JSX?
JSX has a few constraints worth knowing (note: several "limitations" are really just "it's JavaScript, not HTML"):
One root element per return. A component returns a single element. Wrap siblings in a parent, or use a Fragment (
<>...>) to avoid an extraViewin the tree.Expressions, not statements. You can't put
if/for/switchinside{}. Use ternaries,&&, and.map()for conditionals and lists instead.Not HTML — native primitives. In React Native there are no HTML tags; you use
View,Text,Image, etc. (The web'sclassName/htmlForquirk doesn't apply in RN — there's no CSS or DOM at all.)Styling is JS objects. No CSS strings —
styletakes an object (or array of objects), typically fromStyleSheet.create:style={{ padding: 8 }}.Comments must be expressions. Inside JSX, use
{/* like this */}; plain//or `` won't work in the markup.It must compile. JSX isn't valid JS on its own — it's transformed (Babel/Metro) into
jsx()calls before it runs.
The mature framing: most of these aren't real limitations, just consequences of JSX being JavaScript expressions that compile to function calls rather than templating HTML.
Follow-up 1
Can you give an example of a limitation of JSX?
Sure! One limitation of JSX is that it does not support if-else statements. Instead, you can use conditional rendering to achieve similar functionality. Here's an example:
function Greeting(props) {
if (props.isLoggedIn) {
return <h1>Welcome back!</h1>;
}
return <h1>Please sign up.</h1>;
}
Follow-up 2
How can you overcome these limitations?
To overcome the limitations of JSX:
- To return multiple elements, you can wrap them in a parent element. For example:
function App() {
return (
<div>
<h1>Hello</h1>
<p>World</p>
</div>
);
}
- To use if-else statements, you can use conditional rendering. For example:
function Greeting(props) {
if (props.isLoggedIn) {
return <h1>Welcome back!</h1>;
}
return <h1>Please sign up.</h1>;
}
To use HTML-like attributes, you need to use the JSX equivalents. For example, use className instead of class and htmlFor instead of for.
To use inline styles, you need to pass a JavaScript object with CSS properties as the value of the style attribute. For example:
function App() {
const styles = {
color: 'red',
fontSize: '20px'
};
return <h1>Hello</h1>;
}
- To add comments, you can use JavaScript-style comments outside the JSX expression.
Follow-up 3
Are there any alternatives to JSX in React Native?
Yes, there are alternatives to JSX in React Native. One popular alternative is to use React Native's built-in StyleSheet API to define styles instead of inline styles with JSX. Here's an example:
import { StyleSheet, Text, View } from 'react-native';
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center'
},
text: {
fontSize: 20,
fontWeight: 'bold'
}
});
function App() {
return (
Hello World
);
}
In this example, we define styles using the StyleSheet.create method and then apply them to the components using the style prop.
4. How does JSX handle false, null, undefined, and true values?
JSX ignores false, null, and undefined — they render nothing. true is also ignored (it does not render the text "true"). Only actual strings and numbers get rendered as content.
{false} {/* renders nothing */}
{null} {/* renders nothing */}
{undefined} {/* renders nothing */}
{true} {/* renders nothing */}
{0} {/* renders 0 — numbers DO render */}
This is what makes conditional rendering clean: {isLoading && } shows the spinner or nothing.
The classic gotcha interviewers love: 0 and empty strings still render. So {items.length && } will print 0 when the array is empty (and in React Native, a stray 0/string outside a throws "text must be in a component"). Guard with an explicit boolean instead: {items.length > 0 && }.
Follow-up 1
What is the output when these values are rendered in JSX?
When false, null, or undefined are rendered in JSX, they won't be displayed in the output. Only true will be rendered as 'true'.
Follow-up 2
Why does JSX behave this way with these values?
JSX behaves this way to provide a more intuitive and convenient way of rendering components. By ignoring false, null, and undefined, JSX allows developers to conditionally render components without cluttering the code with conditional statements.
Follow-up 3
Can you give an example of handling these values in JSX?
Sure! Here's an example of handling these values in JSX:
const value = false;
function MyComponent() {
return (
<div>
{value && <p>This will be rendered if value is true</p>}
{!value && <p>This will be rendered if value is false</p>}
{null}
{undefined}
{true}
</div>
);
}
In this example, the output will only contain the paragraph element with the text 'This will be rendered if value is false' and the text 'true'. The null and undefined values won't be rendered.
5. How can you comment in JSX?
Inside JSX markup, you write a comment as a JavaScript expression in curly braces:
{/* This is a JSX comment */}
Hello
Key points interviewers may poke at:
- It's a normal JS block comment (
/* ... */) wrapped in{}because everything inside JSX children must be an expression. HTML-style `does **not** work, and a bare//line comment inside{}` will break the closing brace. - Above or outside JSX (in regular code), use ordinary
//or/* */comments as usual. - To comment inside a tag's props, you can also use
{/* ... */}between attributes.
Follow-up 1
What is the syntax for commenting in JSX?
The syntax for commenting in JSX is { /* comment here */ }. You can place the comment anywhere within the JSX code.
Follow-up 2
Can you give an example of a comment in JSX?
Sure! Here's an example of a comment in JSX:
<div>
{/* This is a comment */}
<h1>Hello, World!</h1>
</div>
Follow-up 3
Why is the commenting syntax in JSX different from regular JavaScript?
The commenting syntax in JSX is different from regular JavaScript because JSX is a syntax extension for JavaScript and it needs to be compiled into regular JavaScript code. The curly braces and the /* */ syntax are used to ensure that the comments are ignored during the compilation process and do not affect the resulting JavaScript code.
Live mock interview
Mock interview: JSX in React Native
- Read your scene and goals
- Talk it out; goals tick off live
- Get a score and stronger lines
Your voice and your AI key never touch our servers; the key stays in this browser and is sent only to Google. Only your round scores are saved to track progress.