Click here to Skip to main content
14,085,522 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


8 bookmarked
Posted 3 Apr 2019
Licenced MIT

How to Implement Data Polling with React, Redux, and Thunk

, 17 Apr 2019
Rate this:
Please Sign up or sign in to vote.
Example of data polling with React, Redux and Thunk


In my previous article, How to load Data in React with Redux-Thunk, Redux-Saga, Suspense & Hooks, I compared different ways of loading data from the API. Quite often in web applications, data needs to be updated frequently to show relevant information to the user. Short polling is one of the ways to do it. Check out this article for more details and alternatives.

Briefly, we are going to ask for new data every N milliseconds. We then show this new data instead of previously loaded data. This article gives an example of how to do it using React, Redux, and Thunk.

Let’s define the problem first.

A lot of components of the web site poll data from the API (for this example, I use public API of to show stock prices) and show this data to the user. Polling logic should be separated from the component and should be reusable. The component should show an error if the call fails and hide previously shown errors if the call succeeded.

This article assumes that you already have some experience with creating React/Redux applications.

The code of this example is available on GitHub.

Project Setup

I assume that you already have a React project created using create-react-app with Redux configured and ready to use. If you have any difficulties with it, you can check out this and/or this.

React-redux-toastr is used for showing a popup with the error.

Store Configuration

Redux store configuration with thunk middleware.


import { applyMiddleware, createStore, compose } from 'redux';
import thunk from 'redux-thunk';
import rootReducer from './rootReducer';

export function configureStore(initialState) {
  return createStore(rootReducer, initialState, compose(applyMiddleware(thunk)));

Root Reducer

In rootReducer, we combine reducer with application data (will be created later) and toastr reducer from react-redux-toastr.


import {combineReducers} from 'redux';
import data from './reducer';
import {reducer as toastr} from 'react-redux-toastr';

const rootReducer = combineReducers({

export default rootReducer;


We need just one action to load stock prices. If the call succeeded, then LOAD_DATA_SUCCESS action is dispatched to update global state and remove error (if the previous call failed), otherwise, an error is shown.


import {toastr} from "react-redux-toastr";

export const loadPrices = () => dispatch => {
    return fetch(
        '<a data-href=",
         msft,googl,amzn&types=quote%27" rel="nofollow noopener" target="_blank">,fb,tsla,msft,
        .then(response => {
            if (response.ok) {
                return response.json();

            throw new Error(response.statusText);
            data => {
                dispatch({type: LOAD_DATA_SUCCESS, data});
            error => {
                toastr.error(`Error loading data: ${error.message}`);

Application Reducer

Reducer updates the global state.


import {LOAD_DATA_SUCCESS} from "./actions";

const initialState = {
    prices: []

export default function reducer(state = initialState, action) {
    switch (action.type) {
        case LOAD_DATA_SUCCESS: {
            return {
        default: {
            return state;

High Order Component (HOC) to query data

It’s probably the most tricky part. HOC, like connect from Redux, can decorate another component to add some additional functionality. In this case, withPolling receives pollingAction and duration as properties. On componentDidMount component calls pollingAction and schedules calling the same action every n milliseconds (2000 by default). On componentWillUnmount component stops polling. More details about HOC here.


import * as React from 'react';
import {connect} from 'react-redux';

export const withPolling = (pollingAction, duration = 2000) => Component => {
    const Wrapper = () => (
        class extends React.Component {
            componentDidMount() {
                this.dataPolling = setInterval(
                    () => {

            componentWillUnmount() {

            render() {
                return <Component {...this.props}/>;

    const mapStateToProps = () => ({});

    const mapDispatchToProps = {pollingAction};

    return connect(mapStateToProps, mapDispatchToProps)(Wrapper())

Example of Usage (PricesComponent)

PricesComponent use withPolling, maps prices from state to props and shows data.


import * as React from 'react';
import {connect} from 'react-redux';
import {loadPrices} from "./actions";
import {withPolling} from "./withPolling";

class PricesComponent extends React.Component {
    render() {
        return (
                        <th>Company Name</th>
                    {Object.entries(this.props.prices).map(([key, value]) => (
                        <tr key={key}>
                            <td>{(new Date(Date(value.quote.latestUpdate))).toLocaleTimeString()}</td>

const mapStateToProps = state => ({

const mapDispatchToProps = {};

export default withPolling(loadPrices)(
    connect(mapStateToProps, mapDispatchToProps)(PricesComponent));


The application consists of PricesComponent and ReduxToastr (component to show errors).


import React, {Component} from 'react';
import ReduxToastr from 'react-redux-toastr'
import 'react-redux-toastr/lib/css/react-redux-toastr.min.css'
import PricesComponent from "./PricesComponent";

class App extends Component {
    render() {
        return (
                <PricesComponent text='My Text'/>

export default App;

The app looks like this with no error:

And like this with an error. Previously loaded data still shown.

withPolling HOC Testing

Enzyme with enzyme-adapter-react-16 is used for testing. The tricky parts here are using jest’s fake timer, creating testAction and WrapperComponent.


import Enzyme from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';

// React 16 Enzyme adapter
Enzyme.configure({ adapter: new Adapter() });


import * as React from 'react';
import {mount} from 'enzyme';
import {withPolling} from './withPolling';
import {configureStore} from "./configureStore";
import {Provider} from "react-redux";


describe('withPolling HOC Tests', () => {
    let store;
    let wrapper;

    const TestComponent = () => (
        <div id='test-component'>
            Test Component

    beforeEach(() => {
        store = configureStore();

    afterEach(() => {

    it('function is called on mount', () => {
        const mockFn = jest.fn();
        const testAction = () => () => {

        const WrapperComponent = withPolling(testAction)(TestComponent);

        wrapper = mount(<Provider store={store}><WrapperComponent/></Provider>);


    it('function is called second time after duration', () => {
        const mockFn = jest.fn();
        const testAction = () => () => {

        const WrapperComponent = withPolling(testAction, 1000)(TestComponent);

        wrapper = mount(<Provider store={store}><WrapperComponent/></Provider>);





This example shows how data polling can be implemented using React, Redux, and Thunk.

As an alternative solution, withPolling can be a class component (Polling for example). In this case, <Polling /> will need to be added to the PricesComponent. I think the solution provided in this article is a bit better because we don’t need to add fake components to the JSX (components that don’t add anything visible) and HOC is a technique to add reusable component logic.

That’s it. Enjoy!


This article, along with any associated source code and files, is licensed under The MIT License


About the Author

Valerii Tereshchenko
Software Developer
United States United States
Full Stack Software Developer with major experience in enterprise software development for different industries.
Have a experience in wide range of technologies:
- JavaScript: React, Redux, TypeScript, Saga, Thunk, Cordova, Jest, Enzyme, Material Design, React-MD, Semantic UI, WebStorm
- .Net: C#, WPF, WCF, Windows Forms
- Java: Spring, Spring Boot, Spring Cloud, Spring Data, microservices, jUnit, Mochito, IntelliJ
- DB: Oracle, SQL Server, PL/SQL

You may also be interested in...

Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01 | 2.8.190518.1 | Last Updated 17 Apr 2019
Article Copyright 2019 by Valerii Tereshchenko
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid