GithubHelp home page GithubHelp logo

infostreams / react-component-state-cache Goto Github PK

View Code? Open in Web Editor NEW
1.0 3.0 0.0 69 KB

Adds a simple caching mechanism so that components can remember their state when they are mounted, unmounted, and re-mounted

JavaScript 100.00%

react-component-state-cache's Introduction

react-component-state-cache

Adds a simple caching mechanism so that components can remember their state when they are mounted, unmounted, and re-mounted.

Useful for when you need to remember (for example) user input in a search field between page navigations, or for any kind of data that is not worth it to store in global state (e.g. whether or not the user collapsed a certain info panel).

The cache is cleared when the user reloads the page / your app, so there is no persistence.

Installation

npm install react-component-state-cache

Then include the <ComponentStateContext /> component somewhere high up in your React DOM, for example like this

import React from 'react'
import {ComponentStateCache} from 'react-component-state-cache'
import {Provider} from 'react-redux' // for example
import MyApp from './MyApp.js'

class AppWrapper extends React.Component {
    render() {
        return (
            <Provider store={this.store}>
                <ComponentStateCache>
                    <MyApp />
                </ComponentStateCache>
            </Provider>
        )
    }
}

Example use

Now that you've installed the library, you can start using it in your components as follows:

import React from 'react'
import { withComponentStateCache } from 'react-component-state-cache'

class X extends React.Component {

    constructor(props) {
        super(props)

        this.state = {
            // anything, really.
        }
    }

    componentDidMount() {
        // Restore the component state as it was stored for key '35' in section 'lesson'
        //
        // You can choose 'section' and 'key' freely, of course.
        this.setState(this.props.componentstate.get('lesson', 35))
    }

    componentDidUnmount() {
         // store this state's component in section 'lesson' with key '35'
        this.props.componentstate.set('lesson', 35, this.state)
    }

}

export default withComponentStateCache(X)

or

export default connect(null, null, null, {forwardRef: true})(withComponentStateCache(X))

I'm sure you could also use this with hooks, but I'm not sure how. Pull requests for documentation welcome.

Cache expiration

You can remove all cached states in a section by calling

this.props.componentstate.remove('lesson')

or you can remove specific states by calling

this.props.componentstate.remove('lesson', 35)

The cache is also cleared whenever the user reloads your app.

react-component-state-cache's People

Contributors

infostreams avatar

Stargazers

 avatar

Watchers

 avatar  avatar  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.