JavaScript in React
Using JavaScript in React
React is a JavaScript library. You can create the following JavaScript expressions in your React projects:
- Variables
- Loops
- Functions
- Conditionals
- Objects
- Strings
- DOM and Window events.
The syntax is a little different in React, but the functionality is the same.
In React part 1, we used className
to apply CSS stylings instead of class
. This is because class
is a JavaScript keyword. React adapted by using className
. Note the use of camelCase.
We can use the onclick
method in React, but we need to apply camel casing to the name: onClick
.
W3 Schools created some nice examples of React Events if you would like to learn more.
JavaScript Variables
We can render variables inside our components by encasing them in curly braces { }
. The curly braces act as escape characters for JSX. They inform the JSX to render the element inside of the brackets.
|
|
name
was declared in line 1, as a JavaScript string. We can render it inside the Hello
function by using curly braces. The curly braces let the JSX and HTML know that we want to use the variable name
and its value. If we left off the curly braces, the word name would appear in the browser. The message would read “Hello! My name is name”.
Template literals
You can use template literals to work with strings. This can be easier than concatenation and more flexible than hard coding. These can be very useful with loops, conditionals, and lists.
Template Literal syntax:
- Encase the entire template literal within
{ }
since it is a JavaScript string - Enclose the entire string in backticks ``
- Variables need to be inside curly braces
{variableNameHere}
$
are escape tokens and introduce each variable within the template literal${variableNameHere}
Should look something like this when you put it all together: {'Hello, ${variableNameHere}'}
|
|
Arrow Functions
Arrow functions are simplified functions. You can use an arrow function as an argument for other functions. They do not affect the global scope of the project. Due to their scope, they can be very useful for defining props
, loops, and conditionals. These functions will return values without using the return
keyword.
Why not only use arrow functions? For long functions. They are great for short expressions but longer functions can be hard to read.
|
|
|
|
Working with Props and JS
Recall from the previous chapter
, props
are arguments passed to and between functions. We can use anything as a props
, since props
are placeholders.
We can use props
to pass data between components.
|
|
We can declare our props. This can make the code more meaningful and easier for humans to understand.
|
|
We can also set default variables for props.
|
|
Lines 9 and 14 in the example above are empty tags. These are Fragments. Fragments allow you to group many elements and treat them as a single element. You will often find fragments in return statements.
Importing and Exporting
Exporting Components
There are multiple ways to export. We recommend you pick one way and remain consistent with it throughout your codebase. It will help with readability.
Option 1: After the function
|
|
Option 2: As part of the declaration
|
|
Importing Components
You can import components to other components, not just App.js
.
In the example above, GreetFriend
and Greeting
are in the same file. We could create separate files for them and import GreetFriend
into Greeting
.
Importing Other Things
You can import non-component portions of your codebase. JSON files are a commonly imported item.
Data is stored in a JSON file. We import to the component. The component is able to find the data. In this example, it is a name. GreetFriend
imports the JSON file and then uses it to complete the greeting message.
|
|
When you import something that you plan to use in a function, be sure to give it a name if none exists. In this example, there is only a file name. We assigned it the name data
. This data
is an object, so we can use dot notation to access the object’s values with the keys.
|
|
Check Your Understanding
What structure is used to contain JavaScript variables?
- Brackets
[ ]
- Parentheses
( )
- Double Quotes
" "
- Curly Braces
{ }
|
|
Is returning the following error:
'title' is not defined no-undef
Which syntax will correctly assign a value to title
?
<p>My favorite book is Dracula</p>
<p>My favorite book is {bookInfo.title}</p>
const BookInfo = () => { return( <p>My favorite book is {title}</p> ); }
function Book(props){ return( <p>My favorite book is {props.title}</p> ); };
How would you rewrite the following arrow function as a regular function?
const SayHello = () => {
return(
<h1>Hello!</h1>
);
}
export default SayHello;
function SayHello(){ return( <h1>Hello!</h1> ); }
function SayHello("Hello"){ return(); }
const SayHello(){ return( <h1>Hello!</h1> ); }
function SayHello(){ return( <h1>{Hello!}</h1> ); }