Computer Science & Engineering
Quick Start To React JS
Quick StartWelcome to the React documentation! This page will give you an introduction to 80% of the React concepts that you will use on a daily basis.You will learnHow to create and nest componentsHow to add m.
Note Description
Quick Start
Welcome to the React documentation! This page will give you an introduction to 80% of the React concepts that you will use on a daily basis.
Welcome to the React documentation! This page will give you an introduction to 80% of the React concepts that you will use on a daily basis.
You will learn
- How to create and nest components
- How to add markup and styles
- How to display data
- How to render conditions and lists
- How to respond to events and update the screen
- How to share data between components
- How to create and nest components
- How to add markup and styles
- How to display data
- How to render conditions and lists
- How to respond to events and update the screen
- How to share data between components
Creating and nesting components
React apps are made out of components. A component is a piece of the UI (user interface) that has its own logic and appearance. A component can be as small as a button, or as large as an entire page.
React components are JavaScript functions that return markup:
function MyButton() {
return (
<button>I'm a button</button>
);
}
Now that you’ve declared MyButton
, you can nest it into another component:
export default function MyApp() {
return (
<div>
<h1>Welcome to my app</h1>
<MyButton />
</div>
);
}
Notice that <MyButton />
starts with a capital letter. That’s how you know it’s a React component. React component names must always start with a capital letter, while HTML tags must be lowercase.
Have a look at the result:
function MyButton() { return ( <button> I'm a button </button> );}
export default function MyApp() { return ( <div> <h1>Welcome to my app</h1> <MyButton /> </div> );}
The export default
keywords specify the main component in the file. If you’re not familiar with some piece of JavaScript syntax, MDN and javascript.info have great references.
React apps are made out of components. A component is a piece of the UI (user interface) that has its own logic and appearance. A component can be as small as a button, or as large as an entire page.
React components are JavaScript functions that return markup:
function MyButton() {
return (
<button>I'm a button</button>
);
}
Now that you’ve declared MyButton
, you can nest it into another component:
export default function MyApp() {
return (
<div>
<h1>Welcome to my app</h1>
<MyButton />
</div>
);
}
Notice that <MyButton />
starts with a capital letter. That’s how you know it’s a React component. React component names must always start with a capital letter, while HTML tags must be lowercase.
Have a look at the result:
The export default
keywords specify the main component in the file. If you’re not familiar with some piece of JavaScript syntax, MDN and javascript.info have great references.
Writing markup with JSX
The markup syntax you’ve seen above is called JSX. It is optional, but most React projects use JSX for its convenience. All of the tools we recommend for local development support JSX out of the box.
JSX is stricter than HTML. You have to close tags like <br />
. Your component also can’t return multiple JSX tags. You have to wrap them into a shared parent, like a <div>...</div>
or an empty <>...</>
wrapper:
function AboutPage() {
return (
<>
<h1>About</h1>
<p>Hello there.<br />How do you do?</p>
</>
);
}
If you have a lot of HTML to port to JSX, you can use an online converter.
The markup syntax you’ve seen above is called JSX. It is optional, but most React projects use JSX for its convenience. All of the tools we recommend for local development support JSX out of the box.
JSX is stricter than HTML. You have to close tags like <br />
. Your component also can’t return multiple JSX tags. You have to wrap them into a shared parent, like a <div>...</div>
or an empty <>...</>
wrapper:
function AboutPage() {
return (
<>
<h1>About</h1>
<p>Hello there.<br />How do you do?</p>
</>
);
}
If you have a lot of HTML to port to JSX, you can use an online converter.
Adding styles
In React, you specify a CSS class with className
. It works the same way as the HTML class
attribute:
<img className="avatar" />
Then you write the CSS rules for it in a separate CSS file:
/* In your CSS */
.avatar {
border-radius: 50%;
}
React does not prescribe how you add CSS files. In the simplest case, you’ll add a <link>
tag to your HTML. If you use a build tool or a framework, consult its documentation to learn how to add a CSS file to your project.
In React, you specify a CSS class with className
. It works the same way as the HTML class
attribute:
<img className="avatar" />
Then you write the CSS rules for it in a separate CSS file:
/* In your CSS */
.avatar {
border-radius: 50%;
}
React does not prescribe how you add CSS files. In the simplest case, you’ll add a <link>
tag to your HTML. If you use a build tool or a framework, consult its documentation to learn how to add a CSS file to your project.
Displaying data
JSX lets you put markup into JavaScript. Curly braces let you “escape back” into JavaScript so that you can embed some variable from your code and display it to the user. For example, this will display user.name
:
return (
<h1>
{user.name}
</h1>
);
You can also “escape into JavaScript” from JSX attributes, but you have to use curly braces instead of quotes. For example, className="avatar"
passes the "avatar"
string as the CSS class, but src={user.imageUrl}
reads the JavaScript user.imageUrl
variable value, and then passes that value as the src
attribute:
return (
<img
className="avatar"
src={user.imageUrl}
/>
);
JSX lets you put markup into JavaScript. Curly braces let you “escape back” into JavaScript so that you can embed some variable from your code and display it to the user. For example, this will display user.name
:
return (
<h1>
{user.name}
</h1>
);
You can also “escape into JavaScript” from JSX attributes, but you have to use curly braces instead of quotes. For example, className="avatar"
passes the "avatar"
string as the CSS class, but src={user.imageUrl}
reads the JavaScript user.imageUrl
variable value, and then passes that value as the src
attribute:
return (
<img
className="avatar"
src={user.imageUrl}
/>
);
Tags:
Top Similar Notes
Java Programming
- Subject Name : Java Programming | Subject Code : R20A0508
Introduction Everywhere you look in the real world you see objects—people, animals, plants, cars, planes, buildings, computers and so on. Humans think in terms of objects. Telephones, houses, traffi...
- 1 Likes |
- 29 Downloads |
- 152 Views
Python Programming
- Subject Name : PYTHON PROGRAMMING | Subject Code : [R17A0554
INTRODUCTION DATA, EXPRESSIONS, STATEMENTS Introduction to Python and installation, data types: Int, float, Boolean, string, and list; variables, expressions, statements, precedence of operators, co...
- 0 Likes |
- 36 Downloads |
- 60 Views
Software Testing
- Subject Name : SOFTWARE TESTING | Subject Code : R18A1251
INTRODUCTION Software engineering is an engineering branch associated with development of software product using well-defined scientific principles, methods and procedures. The outcome of software ...
- 0 Likes |
- 10 Downloads |
- 49 Views
Software Engineering
- Subject Name : SOFTWARE ENGINEERING | Subject Code : R18A0511
INTRODUCTION: Software Engineering is a framework for building software and is an engineering approach to software development. Software programs can be developed without S/E principles and methodol...
- 0 Likes |
- 16 Downloads |
- 55 Views
Structural Analysis
- Subject Name : Structural Analysis | Subject Code : 22CSHCT2.1
1.1 Introduction The structure of a building (or other object) is the part which is responsible for maintaining the shape of the building under the influence of the forces, loads and other environme...
- 0 Likes |
- 10 Downloads |
- 49 Views