React!

A javascript library for building user interfaces!

What you'll need:

Tools you'll want:


Getting Started:

We're going to start with create react app : Create React App is an

Create an App!

While there are a lot of files contained in here, just take a look at the package json and type the starting script:

And assuming you've got nothing else running on port 3000, go to: http://localhost:3000/


Delete the src!

There's too much to start with. Let's delete everything:

And create a new landing page: index.js


New SRC Materials:

Inside of index.js, type:


What's going on??

Inside the ReactDOM.render() we've got a React element for the first parameter, and a DOM element for the second.


We have a component:

This is a functional component (also known as a stateless function). There are class components, but those are deprecated.


Below the component, we have:

React uses the virtual dom. This creates a component hierarchy, renders those components, and then inserts them into the DOM where you tell the function. In our case, we're inserting our <HelloWorld/> at #root.


THE DOM!


JSX

Remember our:

The in line HTML looking code is our JSX.


JSX

JSX is really a syntactic sugar for React's createElement():

So, our Hello world function is:


JSX


JSX

Notice that the children parameter looks like it has the spread operator. That meants that the component can have any number of children (remember the DOM).

So we can write:


JSX: Nesting:

Just like HTML, you'll want to have components inside of components:


JSX: Nesting

You may be wondering why we didn't just add <GoodBye\> after the <div>HELLO WORLD (or literally anything else you want to write here)</div>:


JSX Nesting:

The error received was:

When looking at this, it really makes not real sense at first glance. But let's try rewriting this with our React.createElement() function:


OR =>


OR =>

It may make a little more sense like:


What's happening with BonjourMonde?

Javascript does not allow for you to pass back tuples, so you'll need to wrap all of your returned JSX in either a fragment, or some other tag.


JSX- Wrap it with a Fragment

The above code will render to the DOM as:


Keeping your DOM Clean

It's not always the case that you'll want to have your DOM be filled with a bunch of wrapper elements such as <div>s. As of React 16.2, it's possible to use a fragment which acts as a disappearing wrapper:

Now our DOM looks like:


Adding Javascript inside JSX:

Sometimes you'll either want or need (many opinions about that need) to run some javascript inside your JSX.


Adding Javascript inside JSX:

You can access the javascript values by wrapping them within curly braces:


Adding Javascript inside JSX:

Note: Whatever values you wrap within the curly braces must resolve to an actual value, otherwise, nothing will return:

A good rule of thumb is: If you can assign it to a variable, then it's likely able to be rendered in JSX.


Adding Javascript inside JSX: Conditionals

Suppose you wanted to have some material that displayed conditionally?

You can't write:


Adding Javascript inside JSX: Conditionals

There are ways to get around this:


Adding Javascript inside JSX: Conditionals

Another method of using conditionals within your application, such as a login button is to make use of short circuiting:


PROPS

When creating our own HTMLesque components, we need a way to pass data to them. HTML elements take in attributes. For React, we'll take in properties, or props.

Props work just like a function parameter (in fact, on the component side of things, that's exactly what they are). When using JSX, however, you pass the data, just as if you're passing something to an attribute.

Unlike parameters in functions, props are read only and cannot be mutated. The flow of data always comes from the parent to the child.


Props: Passing and Reading


Props: Passing and Reading

Programatically, this allows for the ability to pass elements, both from the parent level to child elements:


Props: Passing and Reading Props

{" "} Notice that the parameter of bonjour has been destructured to name? That's because the passed props act as the keys and the passed data acts as the values in a key value pairing.{" "}

Props: Behind the scenes

Taking a deeper look at props (via the React.createElement() function):


Props: Small Componenets

What's highly recommended is using JSX elements with props to keep your workspace clean:


Props: Dataflow and Communication

While dataflow with props is unidirectional, due to the nature of Javascript, there is a way for child elements to communicate with their parents via props (hint: think first class functions and scoping):


Props: Dataflow and Communication

It looked as if the above was immediate, let's slow things down with a button:


Children: Think of them!

JSX supports nested components just like HTML, but how does that work? Let's take a look at the createElement function again:

Notice that the children have been spread. That means that ultimately, we can have as many child elements as we please (but we'll start with just one, for now).


Children: Think of them!

When the children are passed through the function, nothing is immediately done with them. For example:


Children: Dealing with them

Because JSX elements are not the exact same things as HTML, we can't just write JSX like HTML without some extra steps.

To deal with child elements, we can treat them similarly to props:


Children: Dealing with Multiple:

When dealing with multiple children, you can see that they're passed as an array:


Children: Dealing with Multiple:

You can access specific children by their element:


Children: Dealing with One or Multiple

Children are JSX objects. If need be you can work with what's inside of them (though, in all actuality, you probably shouldn't, or should do it elsewhere, but it's good to know if you're in a pinch):


What about Props WITH Children?

What happens if we pass in both props AND children? The solution is rather counterintuitive:


What about Props WITH Children?

In the TextComponent javascript, we're only passing in one single object.


What about Props WITH Children?

So, ultimately, while we say "props", it's really just an object from which we can destructure any of our props, along with any of our children.


Prop Types:

When you're coding JSX, you'll almost inevitably forget to pass a prop at some given point. What ends up happening is that your prop becomes undefined:

Here we forgot to pass whatever as a prop, and ultimately it ended up undefined.


Prop Types:

There are a number of ways to get around this, but the one that's build into react is PropTypes. These enforce that we're pulling in the proper props!

Depending on what version of react you're using, you may need to install prop-types with (using create-react-app, you should be fine):


Prop Types: Example


Prop Types

The one thinig to note is that, while prop types will give you a warning in your developer console, it will only work if you remember to implement them.