New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@clxx/effect

Package Overview
Dependencies
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@clxx/effect

react effect for cl-utils

  • 0.0.9
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
3
decreased by-25%
Maintainers
1
Weekly downloads
 
Created
Source

引用

import { useGlobalState, setGlobalState, getGlobalState } from "cl-util";

第一步

任意位置初始化并设置全局状态

// setGlobalState的函数签名是一个键值对,键和值类型都任意
// setGlobalState(key: any, value: any)

setGlobalState("ticker-test", 1);

// 甚至可以
setGlobalState(Symbol(), {a:1, b: "hello", c: []})

第二步

在用到全局状态的组件中调用自定义Hooks


// useGlobalState的返回类型和setState类似,但是
// useGlobalState的参数是一个与setGlobalState对应的键

function MyComponent(){
  const [ticker, setTicker] = useGlobalState("ticker-test");
  
  return (
    <div>
      <button 
        onClick={()=>{
          setTicker(ticker+1);
        }}
      >
        点击更新ticker值
      </button>
      <p>当前{ticker}</p>
    </div>
  );
}

其他

只要setGlobalState被初始化过了,接下来可以在任意地方调用,甚至可以在组件之外调用

// 假设当前代码位于组件体之外
setInterval(()=>{
  const ticker = getGlobalState("ticker-test");
  setGlobalState("ticker-test", ticker + 1);
}, 1000)

Keywords

FAQs

Package last updated on 07 Jan 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc