Created
September 25, 2024 11:38
-
-
Save harilee1325/05ac061a64c0bbf154b0fb5574872183 to your computer and use it in GitHub Desktop.
React Native JSX
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
JSX (JavaScript XML) is a syntax extension for JavaScript that looks similar to HTML or XML. In React Native (as well as React), JSX is used to describe the UI components and structure. It allows developers to write UI components using a syntax that resembles HTML but is ultimately transformed into JavaScript. | |
Key Points about JSX in React Native: | |
Looks Like HTML: JSX syntax resembles HTML, making it intuitive for developers familiar with web development. | |
jsx | |
Copy code | |
const MyComponent = () => { | |
return ( | |
<View> | |
<Text>Hello, World!</Text> | |
</View> | |
); | |
}; | |
In the example above, View and Text are React Native components, and JSX allows you to nest them similarly to HTML tags. | |
Not HTML: JSX is not actual HTML but a syntactic sugar for JavaScript. Under the hood, JSX is converted to JavaScript function calls (like React.createElement) that define the structure and behavior of the UI components. | |
For example, this JSX: | |
jsx | |
Copy code | |
<Text>Hello, World!</Text> | |
gets transformed into: | |
javascript | |
Copy code | |
React.createElement(Text, null, "Hello, World!"); | |
Embedding Expressions: You can embed JavaScript expressions inside JSX using curly braces {}. This is useful for dynamic content, function calls, or conditional rendering. | |
jsx | |
Copy code | |
const name = "John"; | |
const Greeting = () => { | |
return <Text>Hello, {name}!</Text>; | |
}; | |
Must Return a Single Root Element: In React Native, each component that uses JSX must return a single root element. You can use View or Fragment to wrap multiple elements. | |
jsx | |
Copy code | |
const MyComponent = () => { | |
return ( | |
<View> | |
<Text>Welcome</Text> | |
<Text>to React Native</Text> | |
</View> | |
); | |
}; | |
Props and Attributes: JSX allows you to pass props (properties) to components by using a syntax similar to HTML attributes. | |
jsx | |
Copy code | |
const MyComponent = () => { | |
return <Button title="Click me" onPress={() => alert('Button clicked!')} />; | |
}; | |
Styles: Styling in React Native is handled using the style attribute in JSX, similar to the style attribute in HTML. However, instead of using strings for CSS properties, you pass a JavaScript object. | |
jsx | |
Copy code | |
const styles = { | |
text: { | |
color: 'blue', | |
fontSize: 20, | |
}, | |
}; | |
const MyComponent = () => { | |
return <Text style={styles.text}>Styled Text</Text>; | |
}; | |
Why JSX in React Native? | |
Declarative Syntax: JSX allows you to describe the structure of the UI declaratively. You define what the UI should look like, and React Native handles the rendering. | |
Component-based Structure: JSX works well with the component-based architecture of React Native, making it easy to break down complex UIs into smaller, reusable pieces. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment