React 16
React 16 was released on September 26, 2017 with exciting changes. For starters React 16 is released under the MIT license which means that developers would no longer have to worry about issue revolving patent grants. This is huge as in the past where companies had no patent protect should they use the React software causing many to reconsider their use of React. This is one less thing to worry about! Let check out some of the amazing feature released with React 16.
Error Boundaries
In React 16, error boundaries can be used the catch any JavaScript errors in the child component tree, log those errors, and display a fallback UI. This allows you to provide a back up should something go wrong and prevent the entire app from breaking. This can be done with the new life cycle component 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:<ErrorBoundary>
<MyWidget />
</ErrorBoundary>
Portals
We all know in React that a parent component can render its child component. But what if we want to render a component that is not within the DOM hierachy of the parment component? This is where Portals come in! Portals provide a first-class way to render children into a DOM node that exists outside the DOM hierarchy of the parent component.
Get Brett Schules’s stories in your inbox
Join Medium for free to get updates from this writer.
You can do this by adding a second element in the root div.
ReactDOM.render(<App />, document.getElementById('root'));
<body>
<div id="root"></div>
<div id="my-portal"></div>
<body>Then, we can use the ReactDOM.createPortal(child, container) to render our component. The function’s first argument child is the content you want to render, and the second argument container is the DOM element where the content is going to render.
class Modal extends Component {
render() {
return (
ReactDOM.createPortal(
this.props.children,
document.getElementById('my-portal')
)
);
}
}Now, since our Modal component returns a portal via createPortal(), we can place this component anywhere in our app.
class App extends Component {
render() {
return (
<div>
<header>
<h1>Welcome to my App!</h1>
</header>
<Modal />
</div>
);
}
}Preventing page rendering with null
This has to be one of my favorite feature in React 16 due it’s usefulness in many cases. This feature allows you to control whether the component get updated or not. This is to prevent unnecessary updates which could hind performance. You can do this by simply setting the state to null which will not trigger a re-render to the component. For example, let’s say we want to prevent useless render if we are getting the same state, we can do this by the example below. Here, we check to see if the state is the same as our current state which then set the state to null if it is. If it is not, then we set the state to our new state. This determines whether if our component re-renders.
getCourse = course => {
const newTitle = course.title;
this.setState(state => {
if (state.title === newTitle) {
return null;
} else {
return { title: newTitle };
}
});
};Rendering Arrays
In React 16, you can render multiple sibling elements without a wrapping element by returning an array.
const MyFavoriteFoods = () => {
return [
<li key="1">Pizza</li>,
<li key="2">Tacos</li>,
<li key="3">Bacon</li>
];
}You can render the then Languages component in a list.
const Foods = () => {
return (
<ul>
<li>Ice Cream</li>
<MyFavoriteFoods />
<li>Apples</li>
<li>Blueberries</li>
</ul>
);
}This would display to the HTML as
· Ice Cream· Pizza· Tacos· Bacon· Apples· Blueberries
This blog was to highlight some of the key features of React 16. Please check out the documentation for a comprehensive list of all of the features with React 16 https://reactjs.org/blog/2017/09/26/react-v16.0.html. I hope you learn as much as I did from writing this blog!






