All About React Router 4

  • In some ways, earlier versions of React Router resembled our traditional mental model of what an application router “should be” by placing all the routes rules in one place.
  • Here are some JavaScript concepts you need to be familiar with for this article:

    If you’re the type that prefers jumping right to a working demo, here you go:

    Earlier versions of React Router centralized the routing rules into one place, keeping them separate from layout components.

  • For the first, let’s modify our to accommodate the browsing and profile pages for users and products:

    While this does technically work, taking a closer look at the two user pages starts to reveal the problem:

    New API Concept: is given to any component rendered by .

  • Here’s a different approach which is better:

    Instead of four routes corresponding to each of the user’s and product’s pages, we have two routes for each section’s layout instead.

  • For example, both these console logs will output the same value when the browser path is `/users`:

    ES2015 Concept: is being destructured at the parameter level of the component function.

This post is going to dig into to React Router 4, how it’s so different from previous React Router versions, and why that is. My intentions for this article aren’t to rehash the already well-written documentation for React Router 4. I will cover the most common API concepts, but the real focus is on patterns and strategies that I’ve found to be successful.
Continue reading “All About React Router 4”

How to use Presentational/Container components in a Redux app – Jakob Lind

I wrote about when and how to use presentational/container components #reactjs #redux

  • Either present something visual (presentational component) OR fetch data (container component).
  • You can use the container/presentational pattern when using both regular Ajax calls inside your components, or when you are using Redux to fetch data.
  • When you start coding your Redux app, you usually start with a wrapper component that receives the data from the Redux store.
  • It is possible to connect Redux to any React component in your application.
  • In a real world application you might need to do more advanced refactorings of the presentational components.

Have you read about Presentational/Container pattern but are still not sure how it looks when it’s used correctly? When should you use it and when should you NOT use it?
Continue reading “How to use Presentational/Container components in a Redux app – Jakob Lind”

Catching exceptions using Higher Order Components in React 16

Catching exceptions using Higher Order Components in #ReactJS 16:  by @notgiorgi #JavaScript

  • So it would be impractical to rewrite the whole component libraries to provide error handling, others may prefer having their views to be “dumb” and represent the UI solely without extra logic.
  • So the better way to go would be wrapping components, reusing wrappers and avoiding modifying the component definitions.So we need to implement this new error handling feature so that:We can display fallback components (views) when the errors happenWe avoid coupling of normal and fallback componentsWe can easily report errors to some serviceWe have ability to reuse reporting or fallback logicNaive, less practical approach would be to do it like this:We could make this slightly better if we parameterize ErrorHandler and instead of calling reportErrorToService directly pass error reporter function as a prop and instead of returning inline fallback view (line 12) we pass a component.
  • But as we already discussed, we don’t want to modify our component libraries, we want to just decorate them.To accomplish this, we could use Higher Order Components:We have this withErrorHandler HOC (We’ll talk about the implementation later), which takes error reporter as callback and fallback component and in case of error, calls the callback and renders the fallback component.
  • We can wrap any component using it and the component will get the error handling, without changing it’s definition, without touching the JSX views.We can make it even prettier, we will probably have one service for error reporting and display mostly the same views as fallback components.
  • So we can export partially applied withErrorHandler HOC like this:and then use it to export wrapped components, instead of just components:This way we can easily add error handling logic to our components and what is also important, we can also easily remove it!So how does the withErrorHandler HOC work?

As you might already know, React 16 beta is out and it ships with a full internal rewrite of the React core (a.k.a Fiber), providing several new features. Though in this post we will only talk about…
Continue reading “Catching exceptions using Higher Order Components in React 16”

8 things to learn in React before using Redux

  • A component can manage a whole lot of state, pass it down as props to its child components and pass a couple of functions along the way to enable child components to alter the state in the parent component again.
  • Component A is the only component that manages local state but passes it down to its child components as props.
  • In addition, it passes down the necessary functions to enable B and C to alter its own state in A.

    Now, half of the local state of component A is consumed as props by component C but not by component B.

  • When you lift the local state management down to component C, all the necessary props don’t need to traverse down the whole component tree.
  • When a library such as Redux “connects” its state managements layer with React’s view layer, you will often run into a higher order component that takes care of it (connect HOC in react-redux).

Facts about React that should be known before using Redux (or MobX). Most important: Learn React first, then opt-in Redux…
Continue reading “8 things to learn in React before using Redux”

8 things to learn in React before using Redux

  • A component can manage a whole lot of state, pass it down as props to its child components and pass a couple of functions along the way to enable child components to alter the state in the parent component again.
  • Component A is the only component that manages local state but passes it down to its child components as props.
  • In addition, it passes down the necessary functions to enable B and C to alter its own state in A.

    Now, half of the local state of component A is consumed as props by component C but not by component B.

  • When you lift the local state management down to component C, all the necessary props don’t need to traverse down the whole component tree.
  • When a library such as Redux “connects” its state managements layer with React’s view layer, you will often run into a higher order component that takes care of it (connect HOC in react-redux).

Facts about React that should be known before using Redux (or MobX). Most important: Learn React first, then opt-in Redux…
Continue reading “8 things to learn in React before using Redux”

How to use React’s Provider Pattern

  • Therefore you could use React’s context to give every component access to the colored theme.
  • That way every component that needs to be styled according to the colored theme could get the necessary information from React’s context object.
  • You have the Provider component that makes properties accessible in React’s context and components that consume the context.
  • This Provider component uses React’s context to pass down the state implicitly.
  • You provide the state to your component as props, the component wraps it into React’s context and all child components can access the state by using a higher order component such as from the react-redux library.

This article gives you a walkthrough for React’s provider pattern. After reading it, you should be able to implement…
Continue reading “How to use React’s Provider Pattern”

Using Higher Order Components in a React Application

  • If you have started using ReactJS recently often you might have felt that you are performing repeated logic in many components.
  • Well Higher Order Component (HOC) provides solution to such a kind of problems.
  • A function (Functional component in React) which takes a component and gives back another component.
  • So the exact benefits are:

    Functional components started from React V15.

  • But apart from that if we are careful enough we can have an architecture which can abstract logic that can be shared across other presentational components using HOC has great benefits.

If you have started using ReactJS recently often you might have felt that you are performing repeated logic in many components. For example consider an app having:
Continue reading “Using Higher Order Components in a React Application”