插件

插件(Plugins)

LocalStorage

import localStoragePlugin from "kea-localstorage";
import { resetContext } from "kea";

resetContext({
  plugins: [localStoragePlugin],
});

localStoragePlugin({
  // in case you want to replace this, e.g. for tests or non browser environments
  storageEngine: window.localStorage,

  // added before all paths in localStorage keys
  prefix: "example",

  // to change the symbol that concats path parts
  separator: "_",
});
const logic = kea({
  // path must be defined!
  path: () => ["scenes", "homepage", "name"],

  actions: ({ constants }) => ({
    updateName: (name) => ({ name }),
  }),

  reducers: ({ actions, constants }) => ({
    // just add { persist: true }
    name: [
      "chirpy",
      { persist: true },
      {
        [actions.updateName]: (state, payload) => payload.name,
      },
    ],
    // you may override the prefix and separator keys
    name: [
      "chirpy",
      { persist: true, prefix: "example", separator: "_" },
      {
        [actions.updateName]: (state, payload) => payload.name,
      },
    ],
  }),
});