The Front-End Web Development with React is an online course to advance you with the latest web development trends alongside the hottest technology that is simple and easy to learn. Learn how to use React, Redux, and modern JavaScript to build powerful and fast React applications from the ground up using Webpack, Babel, Redux and more.
The course will start with training you in designing basic web pages, creating user-friendly website pages and styling and animations for the web pages and to make you an expert front-end web developer. Big companies such as Netflix, Apple, Airbnb, Instagram, Pinterest and many others are regular users of React JS, making this framework the technology of the future.
1: React is all about components
React is designed around the concept of reusable components. You define small components and you put them together to form bigger components.
All components small or big are reusable, even across different projects.
The component name starts with a capital letter. This is required since we will be dealing with a mix of HTML elements and React elements. Lowercase names are reserved for HTML elements. In fact, go ahead and try to name the React component just “button” and see how ReactDOM will ignore the function and renders a regular empty HTML button.
Every component receives a list of attributes, just like HTML elements. In React, this list is called props. With a function component, you can name it anything though.
We weirdly wrote what looks like HTML in the returned output of the Button function component above. This is neither JavaScript nor HTML, and it’s not even React.js. But, it’s so popular that it became the default in React applications. It’s called JSX and it’s a JavaScript extension. JSX is also a compromise! Go ahead and try and return any other HTML element inside the function above and see how they are all supported
2: What the flux is JSX?
The createElement function is the main function in the React top-level API. It’s 1 of a total of 8 things in that level that you need to learn. That’s how small the React API is.
Much like the DOM itself having a document.createElement function to create an element specified by a tag name, React’s createElement function is a higher-level function that can do what document.createElement does, but it can also be used to create an element to represent a React component. We did the latter when we used the Button component.
3: You can use JavaScript expressions anywhere in JSX
Any JavaScript expression can go inside those curly braces. This is equivalent to the ${} interpolation syntax in JavaScript template literals.
This is the only constraint inside JSX: only expressions. So, for example, you can’t use a regular if statement, but a ternary expression is ok.
JavaScript variables are also expressions, so when the component receives a list of props (the RandomValue component didn’t, props are optional), you can use these props inside curly braces. We did this in the Button component.
JavaScript objects are also expressions. Sometimes we use a JavaScript object inside curly braces, which makes it look like double curly braces, but it’s really just an object inside curly braces. One use case of that is to pass a CSS style object to the special style attribute in React.
4: You can write React components with JavaScript classes
This is the reason why we used this.props.label inside the JSX in the rendered output above. Because every element rendered through a class component gets a special instance property called props that holds all values passed to that element when it was created.
Since we have an instance associated with a single use of the component, we can customize that instance as we wish. We can, for example, customize it after it gets constructed by using the regular JavaScript constructor function.
5: Events in React: Two Important Differences
When handling events inside React elements, there are two very important differences from the way we do so with the DOM API:
All React elements attributes (events included) are named using camelCase, rather than lowercase. It’s onClick, not onclick.
We pass an actual JavaScript function reference as the event handler, rather than a string.
6: Every React component has a story
First, we define a template for React to create elements from the component.
Then, we instruct React to use it somewhere. For example, inside a render call of another component, or with ReactDOM.render.
Then, React instantiates an element and gives it a set of props that we can access with this.props. Those props are exactly what we passed in step 2 above.
Since it’s all JavaScript, the constructor method will be called (if defined). This is the first of what we call: component lifecycle methods.
React then computes the output of the render method (the virtual DOM node).
Since this is the first time React is rendering the element, React will communicate with the browser (on our behalf, using the DOM API) to display the element there. This process is commonly known as mounting.
7: React components can have a private state
In the render method, we used the two properties we have on the state with a normal read syntax. There is no special API for that.
Now, notice that we updated the state using two different ways:
- By passing a function that returned an object. We did that inside the handleClick function.
- By passing a regular object. We did that inside the interval callback.
Both ways are acceptable, but the first one is preferred when you read and write to the state at the same time (which we do). Inside the interval callback, we’re only writing to the state and not reading it. When in doubt, always use the first function-as-argument syntax. It’s safer with race conditions because setState should always be treated as an asynchronous method.
How do we update the state? We return an object with the new value of what we want to update. Notice how in both calls to setState, we’re only passing one property from the state field and not both. This is completely okay because setState actually merges what you pass it (the returned value of the function argument) with the existing state. So, not specifying a property while calling setState means that we wish to not change that property (but not delete it).
8: React will react
Think of the render function’s input as both:
- The props that get passed by the parent.
- The internal private state that can be updated anytime.
- When the input of the render function changes, its output might change.
React keeps a record of the history of renders and when it sees that one render is different than the previous one, it’ll compute the difference between them and efficiently translate it into actual DOM operations that get executed in the DOM.
9: React is your agent
You can think of React as the agent we hired to communicate with the browser. Take the current timestamp display above as an example. Instead of us manually going to the browser and invoking DOM API operations to find and update the p#timestamp element every second, we just changed a property on the state of the component and React did its job of communicating with the browser on our behalf. I believe this is the true reason why React is popular. We hate talking to Mr. Browser (and the so many dialects of the DOM language that it speaks) and React volunteered to do all the talking for us.
10: Every React component has a story (part 2)
A component might need to re-render when its state gets updated or when its parent decides to change the props that it passed to the component.
- If the latter happens, React invokes another lifecycle method, componentWillReceiveProps.
If either the state object or the passed-in props are changed, React has an important decision to do. Should the component be updated in the DOM? This is why it invokes another important lifecycle method here, shouldComponentUpdate. This method is an actual question, so if you need to customize or optimize the render process on your own, you have to answer that question by returning either true or false.
If there is no custom shouldComponentUpdate specified, React defaults to a very smart thing that’s actually good enough in most situations.
First, React invokes another lifecycle method at this point, componentWillUpdate. React will then compute the new rendered output and compare it with the last rendered output.