001.005.Building our first JSX React Component
Congratulations on reaching this pivotal stage! In this tutorial, you’ll build your first JSX-based React component and integrate it into your project. Along the way, you’ll explore the nuances of JSX, set up React libraries, and understand how React and JSX work behind the scenes. By the end, you’ll have a functioning React component rendering directly to the DOM.
Steps to Build a JSX React Component
1. Understanding React and JSX
React components are the core building blocks of React applications. These components use JSX (JavaScript XML), a syntax extension that resembles HTML but works seamlessly with React. JSX makes it easier to visualize and manage your UI structure within JavaScript files.
2. Install React and ReactDOM
To create and render React components, you need two libraries:
- React: Creates and manages the components.
- ReactDOM: Handles rendering components to the DOM.
Run the following command to install these libraries:
npm install react react-dom
Note: Unlike development tools, React libraries should be installed as production dependencies (without
--save-dev
) because they are required when your app runs.
3. Set Up Your Component
-
Import React and ReactDOM
Add the following imports to your JavaScript file to use React and ReactDOM:import React from 'react'; import ReactDOM from 'react-dom';
-
Prepare JSX for Your Component
JSX is syntactically similar to HTML. To build your first component:- Open your HTML file and copy the relevant markup (e.g., a Bootstrap Jumbotron).
- Paste this markup into your JavaScript file.
Example:
const MyComponent = () => { return ( <div className="jumbotron"> <h1>Welcome to React!</h1> <p>This is your first JSX React component.</p> <button className="btn btn-primary">Click Me</button> </div> ); };
Important: In JSX, use
className
instead ofclass
(a reserved keyword in JavaScript). -
Render Your Component to the DOM
UseReactDOM.render()
to render the component into a specific element in your HTML file.- Add a
div
with anid
in your HTML:<div id="root"></div>
- In your JavaScript, render your component:
ReactDOM.render(<MyComponent />, document.getElementById('root'));
- Add a
4. Handling Inline Styles in JSX
In JSX, inline styles are defined as objects, and property names follow camelCase conventions. For example:
const MyStyledComponent = () => {
return (
<div
style={{
backgroundColor: 'lightblue',
padding: '20px',
textAlign: 'center'
}}
>
<h1 style={{ color: 'darkblue' }}>Styled Component</h1>
</div>
);
};
5. Test Your Component
Start your development server:
npm start
Open your browser and verify that your React component is rendering as expected.
Key Takeaways
- JSX Basics: JSX allows you to write HTML-like syntax in your JavaScript files. It must have a single root element (e.g., a
div
) and useclassName
for CSS classes. - ReactDOM: This library bridges the gap between React components and the actual DOM.
- Inline Styling in JSX: Inline styles must use JavaScript objects and camelCase property names.
- Debugging JSX: Errors in JSX often relate to syntax or reserved keywords (like
class
orfor
).
Next Steps
This tutorial marks your first steps into React development. In the next section, you’ll dive deeper into creating reusable React components, managing state, and exploring ES6 features like destructuring and arrow functions.
Ready to Level Up Your Skills?
Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!
Enroll Now for Free!