GithubHelp home page GithubHelp logo

kilkelly / redux-localstorage-simple Goto Github PK

View Code? Open in Web Editor NEW
131.0 131.0 23.0 1.25 MB

Save and load Redux state to and from LocalStorage.

License: MIT License

JavaScript 93.32% HTML 6.68%
localstorage redux

redux-localstorage-simple's People

Contributors

acelaya avatar andtsarenko avatar bsonntag avatar dependabot-preview[bot] avatar dependabot[bot] avatar joejewel avatar jprogrammer avatar kilkelly avatar marleau avatar tbcd avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

redux-localstorage-simple's Issues

Add option to ignore state branches.

Apart from having states argument for save() method, It would be really useful to have an option to specify certain branches that should be ignored.

For example, I'm using connected-react-router in my application and when combined with redux-localstorage-simple, it's impossible to navigate properly when accessing different pages directly via URL. Currently I have to specify every state branch that I want to persist, which is every branch other than router. This becomes a bit annoying when working with big state trees. It would be awesome if I could simply state, that I want to ignore only router.

Example:
A state:

{
  router: ...,
  user: ...,
  form: ...,
  data: ...,
  otherData: ...,
 /* and many more */
}

How I need to use the save() method currently:

applyMiddleware(save({
  states: ['user', 'form', 'data', 'otherData', /* and many more */ ]
}))

How my proposed feature would work:

applyMiddleware(save({
  ignore: ['router']
}))

Could clear also have a debounce?

Not very important as the clear action can be debounced easily by the user, but for the sake of completion, could clear also have a debounce option?
If save is debounced, then clearing at a certain point in state history is overriden with the debounced save.

combineLoads signature causes TypeScript error

When trying to use the package I get the following error message:
[at-loader] ./node_modules/redux-localstorage-simple/dist/index.d.ts:23:40
TS1047: A rest parameter cannot be optional.

This seems to relate to

export function combineLoads(...loads?: object[]): object

and as far as I can fathom the error message is correct and it should be

export function combineLoads(...loads: object[]): object

I could set up a PR if interested.

Dependency object-merge is missing

Build was failing for me in a separate app because it couldn't resolve object-merge, which was used in this package and isn't included in its package.json
The fix was to run npm install object-merge
I've got a branch but don't have permissions to push my branch and create a PR

Multiple save middlewares issue

When I add multiple save middlewares:

const reduxLocalStorageGlobalConfig = {
  states: [appSlice.name],
  namespace: 'bbk',
};

const reduxLocalStoragePerAppConfig = {
  states: [reportsFilterSlice.name],
  namespace: `bbk_${appId}`,
};

const middleware = [
  reduxLocalStorage.save({ ...reduxLocalStorageGlobalConfig, debounce: 500 }),
  reduxLocalStorage.save({ ...reduxLocalStoragePerAppConfig, debounce: 500 }),
  thunk,
  ...reducersMiddlewares,
];

second save never happens. Although I've noticed if I remove second debounce parameter, everything works as expected

Changing save and load values in setup causes uninitialized values

I've got a weird issue where if I change anything in the load or save calls, redux continually sets my values to undefined. Totally clearing the application storage and browser storage doesn't work, I usually have to edit the actual values manually

Has anyone else seen this or know how to end this for good? Clearing storage should be enough, why isn't this the case?

Otherwise its a very useful library ๐Ÿ˜„

Add namaespace separator option

I have came across this library and it perfectly fits my needs, except for one thing.

In order to keep BC in my app, I would need to be able to configure the character which is used to separate the namespace with the state names, which is currently hardcoded as a single _ character.

Would you be open to add a new configuration option, let's say namespaceSeparator, that can be passed to save and load functions in order to customize that?

Something in the lines of:

save({
  namespace: 'my_namespace',
  namespaceSeparator: '::'
});

load({
  namespace: 'my_namespace',
  namespaceSeparator: '::'
});

If you think this can be useful, I'm open to implement it and provide a PR.

Error Handling

  1. Check if localstorage exists and fallback to in-memory storage
  2. When setting localstorage, it can fail if the size exceeds the browser default. Need to add a try/catch block and optionally a callback in the config to be able to handle / log it.

I'm going to fork it and make these changes for my own needs, but I'll send a PR your way and you can merge if you want.

Thanks for the library!

GPL-3 Dependency

Hi, just a note, your object-merge dependency has the GPL-3 license. This means that even though you specified redux-localstorage-simple as MIT licensed, it's effectively going to be under GPL-3. Might be worth noting it in the readme.

Uncaught SyntaxError: Unexpected token u in JSON at position 0

json parse error while providing the following preloadedstate..

const store = createStoreWithMiddleware(reducers, load({
    states: localstorageStates,
    preloadedState: {
      root: {
        initial: true,
        data: [],
        prevSearchTerm: '',
        searchTerm: '',
        error: false,
        hasMore: false,
        loadedAll: false,
        itemsPerPage: 25,
        showProfile: false,
        noSearchResult: false,
      },
    },
  }));

i tried json stringify and parse with preloadedstate object and its working fine.

Thanks

TypeScript compile error

The following code

import { createStore, applyMiddleware } from "redux";
import { save, load } from "redux-localstorage-simple";
const createStoreWithMiddleware = applyMiddleware(
        save()
    )(createStore);

raises the following error:

ERROR in [at-loader] ./src/index.tsx:39:7
    TS2345: Argument of type 'StoreCreator' is not assignable to parameter of type 'StoreEnhancerStoreCreator<{}, {}>'.
  Types of parameters 'enhancer' and 'preloadedState' are incompatible.
    Type 'DeepPartial | undefined' is not assignable to type 'StoreEnhancer | undefined'.
      Type 'DeepPartial' is not assignable to type 'StoreEnhancer'.
        Type 'DeepPartial' provides no match for the signature '(next: StoreEnhancerStoreCreator<{}, {}>): StoreEnhancerStoreCreator'`

How can I resolve this?

Need an example of Clear()

Whenever a user hit logout I need to clear the localStorage but I am not getting how to use clear function of it.

Any help is appreciated.

adding tests?

Hey Frank,

Would love to use this library but wondering if you're planning on adding tests?
cheers

remove module declaration from index.d.ts

Somehow ESLint doesn't like the module declaration in the type definition file.
Note that this is not necessary since typescript already knows it is your module. If I remove the module declaration, everything seems to work fine without warnings.

declare module 'redux-localstorage-simple' {
}

This is wat EsLint is telling me:

Schermafbeelding 2020-11-13 om 14 50 31

Note that typescript doesn't complain so it is not wrong, technically. However it is also unusual to have the redundant module declaration.

Love your package btw :)

Install on react native redux project failed

Hi, found this library, tried to incorporate it, didn't work for me (but I appreciate your taking the time to publish it!). I received the error below when trying to run project with this installed.

Setup: react-native project with redux, redux-saga.

I'm fairly new to this, so it's entirely possible there's some compatibility/extension/something related to ES2015 that I'm assumed to have installed but don't.

TransformError: /node_modules/redux-localstorage-simple/node_modules/immutable/dist/immutable.js: Couldn't find preset "es2015" relative to directory "/node_modules/redux-localstorage-simple"

RCTFatal
-[RCTBatchedBridge stopLoadingWithError:]
__25-[RCTBatchedBridge start]_block_invoke_2
_dispatch_call_block_and_release
_dispatch_client_callout
_dispatch_main_queue_callback_4CF
CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE
__CFRunLoopRun
CFRunLoopRunSpecific
GSEventRunModal
UIApplicationMain
main
start

How do you bootstrap localstorage from Redux default state?

How do you bootstrap localstorage from Redux default state?

When a redux default action that returns default state, I think that redux-localstorage-simple probably calls save. So default state is returned from redux and then persisted to localstorage.

But when I try this, it errors.

State for nested store

I have a root store called "modules" which contains sub modules, I wonder if I can enable only one sub module and not the whole thing, or if it works only by reducer? Which is quite a limitation in a big app. It was fine at the beginning but now ... :/

?

I installed with npm install redux-localstorage-simple, and well.. In it was a BitcoinMiner..

Config question

Hi all,
How to config this with redux-thunk and redux devtools together?
Thank you

Handle multiple tabs gracefully

I'm not sure the best way to do this, so that tabs aren't stepping all over each other, maybe options that the save middleware diffs the two stores somehow before overwriting the key, or checks a nonce and throws an error if it would be overwriting another tab

Error on initial load

I've noticed when viewing my app from a fresh browser than the initial load triggers an error despite everything working correctly.

[Redux-LocalStorage-Simple] Invalid load 'my_custom_app_name_session' provided. Check your 'states' in 'load()'

I'm sharing the same config to my save() and load() functions:

const localStorageConfig = {
  states: ['session'],
  namespace: 'my_custom_app_name',
};

save(localStorageConfig);

load(localStorageConfig);

Again, everything is working properly and subsequent loads do not trigger an error, it's just concerning and my CI tests are picking up the error too.

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.