Code Monkey home page Code Monkey logo

redux-window's Introduction

redux-window

This is a small package to store the browser window's width and height in state.

Getting started

Install:

npm install --save redux-window

Add the reduxWindow reducer:

import { combineReducers } from 'redux';
import reduxWindow from 'redux-window';

const reducers = combineReducers({
  reduxWindow,
  ...other reducers
});

Then in index.js, import resizeEvent and pass it your store:

import { resizeEvent } from 'redux-window';
import store from './store';

resizeEvent(store);

Optional throttle on dispatch

The resize event is throttled to allow dispatch to be fired a maximum of once every 200ms. To change the default wait time, pass a second parameter to resizeEvent.

resizeEvent(store, 500);

You can optionally pass this value in with an object too:

resizeEvent(store, { wait: 500 });

Useage with Redux and NextJS

When the store is initialised on the server there is no access to the window object. Because of this you can set a default width and height for the initial render and the client will correct these values once it loads.

resizeEvent(store, {
  wait: 500,
  defaultWidth: 1200,
  defaultHeight: 900,
});

Selectors

To get the window width and height from state:

import { getWidth, getHeight } from 'redux-window';

function mapStateToProps(state, ownProps) {
  return {
    width: getWidth(state),
    height: getHeight(state),
  };
}

NOTE: the selectors require your reducer to use the key reduxWindow, as in the example above.

redux-window's People

Contributors

bdmason avatar

Watchers

James Cloos avatar Luke Harris avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.