Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

React Custom Hooks for NodeCG

License

NotificationsYou must be signed in to change notification settings

nodecg/react-hooks

Repository files navigation

This package is a collection custom hooks ofReact Hooks for NodeCG API.

🚨 This package is in alpha state. But feel free to try out and file an issue for suggestion/bugs!

Install

npm install @nodecg/react-hooks# oryarn add @nodecg/react-hooks# orpnpm add @nodecg/react-hooks

About React Hooks

(This section comes from when React hooks was just introduced as an alpha feature, but is still useful to keep as an introduction.)

The React Hooks are a new way ofsharing code between components, introduced in version 16.8.

Please read the documentation of React Hooks thoroughly before using them.

It also helps to learn the background mechanism of React Hooks.React hooks: not magic, just arrays

Recommendation

Useeslint-plugin-react-hooks in your project. It is 100% smarter than you to detect violation of the Rules of Hooks.

Usage

useReplicant

  • Subscribes to specified replicant and returns the value as state.
  • Allows you to use replicant values in function component.
import{useReplicant}from"@nodecg/react-hooks";// This component will re-render when the `counter replicant value changesexportfunctionRunnerName(){const[count,setCount]=useReplicant("counter");return(<div><div>{count}</div><buttononClick={()=>setCount(count+1)}/></div>);}

useListenFor

  • Subscribes messages withlistenFor, and unlistens on unmount.
  • Combining with other hooks enables powerful stateful features with function component
import{useListenFor}from"@nodecg/react-hooks";// Shows modal for 1 second when NodeCG receives 'errorHappened' message from the serverexportfunctionAlertOnMessage(){const[showAlert,setShowAlert]=useState(false);useListenFor("errorHappened",()=>{setShowAlert(true);});useEffect(()=>{if(!showAlert){return;}// Disappear alert 1 second afterconsttimer=setTimeout(()=>{setShowAlert(false);},1000);// Make sure to return cleanup functionreturn()=>{clearTimeout(timer);};},[showAlert]);return<Modalshow={showAlert}/>;}

License

MIT © Keiichiro Amemiya (Hoishin)

About

React Custom Hooks for NodeCG

Topics

Resources

License

Security policy

Stars

Watchers

Forks

Packages

No packages published

Contributors7


[8]ページ先頭

©2009-2025 Movatter.jp