Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
React Cookbook

You're reading from   React Cookbook Create dynamic web apps with React using Redux, Webpack, Node.js, and GraphQL

Arrow left icon
Product type Paperback
Published in Aug 2018
Publisher Packt
ISBN-13 9781783980727
Length 580 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Carlos Santana Roldán Carlos Santana Roldán
Author Profile Icon Carlos Santana Roldán
Carlos Santana Roldán
Arrow right icon
View More author details
Toc

Table of Contents (17) Chapters Close

Preface 1. Working with React 2. Conquering Components and JSX FREE CHAPTER 3. Handling Events, Binding and Useful React Packages 4. Adding Routes to Our Application with React Router 5. Mastering Redux 6. Creating Forms with Redux Form 7. Animations with React 8. Creating an API with Node.js Using MongoDB and MySQL 9. Apollo and GraphQL 10. Mastering Webpack 4.x 11. Implementing Server-Side Rendering 12. Testing and Debugging 13. Deploying to Production 14. Working with React Native 15. Most Common React Interview Questions
16. Other Books You May Enjoy

What's new in React?

This paragraph was written on August 14, 2018, and the latest version of React was 16.4.2. The React 16 version has a new core architecture named Fiber. 

In this recipe, we will see the most important updates in this version that you should be aware of to get the most out of React.

How to do it...

Let's see the new updates:

  1. Components can now return arrays and strings from render: Before, React forced you to return an element wrapped with a <div> or any other tag; now it is possible to return an array or string directly:
    // Example 1: Returning an array of elements.
render() {
// Now you don't need to wrap list items in an extra element
return [
<li key="1">First item</li>,
<li key="2">Second item</li>,
<li key="3">Third item</li>,
];
}

// Example 2: Returning a string
render() {
return 'Hello World!';
}
  1. Also, React now has a new feature called Fragment, which also works as a special wrapper for elements. It can be specified with empty tags (<></>) or directly using React.Fragment:
    // Example 1: Using empty tags <></>
render() {
return (
<>
<ComponentA />
<ComponentB />
<ComponentC />
</>
);
}

// Example 2: Using React.Fragment
render() {
return (
<React.Fragment>
<h1>An h1 heading</h1>
Some text here.
<h2>An h2 heading</h2>
More text here.
Even more text here.
</React.Fragment>
);
}

// Example 3: Importing Fragment
import React, { Fragment } from 'react';
...
render() {
return (
<Fragment>
<h1>An h1 heading</h1>
Some text here.
<h2>An h2 heading</h2>
More text here.
Even more text here.
</Fragment>
);
}
  1. Error boundaries with from the official website:
A JavaScript error in a part of the UI shouldn’t break the whole app. To solve this problem for React users, React 16 introduces a new concept of an "error boundary". Error boundaries are React components that catch JavaScript errors anywhere in their child component tree, log those errors, and display a fallback UI instead of the component tree that crashed. Error boundaries catch errors during rendering, in lifecycle methods, and in constructors of the whole tree below them. A class component becomes an error boundary if it defines a new lifecycle method called componentDidCatch(error, info).
    class ErrorBoundary extends React.Component {
constructor(props) {
super(props);

this.state = {
hasError: false
};
}

componentDidCatch(error, info) {
// Display fallback UI
this.setState({
hasError: true
});

// You can also log the error to an error reporting service
logErrorToMyService(error, info);
}

render() {
if (this.state.hasError) {
// You can render any custom fallback UI
return <h1>Something went wrong.</h1>;
}

return this.props.children;
}
}

// Then you can use it as a regular component:
render() {
<ErrorBoundary>
<MyComponent />
</ErrorBoundary>
}
  1. Better server-side rendering with from the official site:
React 16 includes a completely rewritten server renderer. It's really fast. It supports streaming, so you can start sending bytes to the client faster. And thanks to a new packaging strategy that compiles away process.env checks (Believe it or not, reading process.env in Node is really slow!), you no longer need to bundle React to get good server-rendering performance.
  1. Reduced file size with from the official site: "Despite all these additions, React 16 is actually smaller compared to 15.6.1.
    • react is 5.3 kb (2.2 kb gzipped), down from 20.7 kb (6.9 kb gzipped)
    • react-dom is 103.7 kb (32.6 kb gzipped), down from 141 kb (42.9 kb gzipped)
    • react + react-dom is 109 kb (34.8 kb gzipped), down from 161.7 kb (49.8 kb gzipped)

That amounts to a combined 32% size decrease compared to the previous version (30% post-gzip)."


If you want to check the latest updates on React, you can visit the official React blog: https://reactjs.org/blog.
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime