npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-persist-state

v1.0.5

Published

Persisting React state between route navigation for react SPA | 即使 SPA 页面间跳转仍能使组件保持数据

Downloads

50

Readme

react-persist-state

Persisting React state between route navigation for react SPA | 即使 SPA 页面间跳转仍能使组件保持数据

quick glacne: https://www.youtube.com/watch?v=KfnBAnNu_GI&list=PLM1v95K5B1ntVsYvNJIxgRPppngrO_X4s

usage | 用法

import persist from 'react-persist-state'

const connect = persist({
  defaultState: { test: '' }
})

const TestPersist = ({ testProp, setPersist, persisted }) => {
  return (<div>
    <p>testProp:{JSON.stringify(testProp)}</p>
    <p>persisted.test:<input value={persisted.test} onChange={(e) => {
      setPersist({ test: e.target.value })
    }} /></p>
    <p>persisted.test:{JSON.stringify(persisted.test)}</p>
  </div>)
}

export default connect(TestPersist)

when you route, persisted will keep | 当你切换页面,persisted

example | 示例

git clone https://github.com/postor/react-persist-state.git
cd react-persist-state/examples/basic
yarn && yarn dev

open http://localhost:3000

params | 参数

persist

pass in config and return connect | 传入配置,并返回 connect

import persist from 'react-persist-state'
const connect = persist({
  maxAge: 0,          // timeout(miliseconds) for unmounted persist, 0 means no timeout | 超时时间(毫秒),0表示不限
  defaultState: {},   // default persisted state | 默认 state
  onUpdate: (key, Comp, newState) => {} // use this hook if you need to know each change | 数据更新的钩子
})

// data is stored in memory, if you close browser, data will lost even not reach maxAge

connect

HOC that generates Components can persist | 返回能够保持状态的组件的 HOC

  • Comp the component will need to persist state | Comp 参数为需要保持状态的组件
  • key default is Comp, when you use the same connect and the same Comp multiple times(e.g. list), you need to specify a key for each of the | key 默认为 Comp,如果你使用同一个 connect 多次连接相同的 Comp (例如列表的情况),你需要给每个连接指定一个不同的 key
connect(Comp,key)

[1, 2, 3, 4].map((i) => {
  const InputItemK = connect(InputItem, `InputItem-${i}`)
  return (<InputItemK key={i} />)
})

props.setPersist

update persisted data | 更新保持的数据

  • obj use as reducer if obj is function, else act like setState in react | 如果是函数,则用作reducer,否则和 react 的 setState 行为一致
const TestPersist = ({ setPersist, persisted }) => {
  ...
  <a onClick={()=>setPersist(obj)}>

props.persisted

data that persisited | 保持的数据

const TestPersist = ({ setPersist, persisted }) => {
  ...
  <p>{persisted.something}</p>

clearCache

clear cached data when you need memory efficient | 清理缓存数据

connect.clearCache()