Home

Awesome

redux-webext

Build Status codecov npm version

This package allows you to use Redux for managing the state of your WebExtension.

Installation

npm install redux-webext --save

Introduction

Usually WebExtension consists of two basic parts:

<br/> <p align="center"> <img src="https://cloud.githubusercontent.com/assets/1555792/19413626/dd0f33be-9332-11e6-801f-090ffb8eced4.png"/> </p> <br/>

As you can see, to provide data between background and UI pages you have to use messages. Or... actually, you don't have to, because of redux-webext:

<br/> <p align="center"> <img src="https://cloud.githubusercontent.com/assets/1555792/19413725/21031a42-9336-11e6-85ce-d5dc63104936.png"/> </p> <br/>

In a nutshell, redux-webext takes care of communication between background and UI pages using Redux. But there are 2 key things that you should understand:

The words above don't make a lot of sense without code, right? So, there's tutorial with example where you can find how to use redux-webext and how it works.

Examples

API

createBackgroundStore(options) - creates Redux store for background page.

Options

Returns the provided store.

Example

const store = createStore(reducer); // real Redux store

const backgroundStore = createBackgroundStore({
    store,
    actions: {
        // "INCREMENT_UI_COUNTER" is a string that will be used as a type of action in UI page
        // "incrementUICounter" is an action is background page
        INCREMENT_UI_COUNTER: incrementUICounter,
        DECREMENT_UI_COUNTER: decrementUICounter
    }
});

createUIStore() - creates Redux store for UI pages.

Returns promise which will be resolved after receiving the current state of background store. And an object with identical to Redux store structure will be passed as resolved result.

Example

async function initApp() {
    const store = await createUIStore();

    ReactDOM.render(
        <Provider store={store}>
            <App/>
        </Provider>,
        document.getElementById('app')
    );
}

initApp();