useProjectState()
๐๏ธ Using useProjectState in Modularjs
Modularjs also supports project-specific persistent state using the useProjectState
hook. This hook works similarly to useGlobalState
, but the data is scoped to the current project only.
๐ก What Is useProjectState?
While useGlobalState
shares state across all projects, useProjectState
restricts the shared state to only the current project. This is useful when you need state that should persist between sessions but should not be visible or shared across other projects.
๐งช Example: Project-Specific Counter
Here, the count
state is saved only for the current project. Opening another project won't affect or display this state โ each project has its own isolated count
value.
๐ Syntax
๐
'unique-key'
is required. It uniquely identifies the value within the project.๐ฅ
defaultValue
is used only if no saved value exists yet for that key.
โ
Why Use useProjectState?
๐งณ Project Isolation: Keeps data separate between projects.
๐พ Persistent: Saves automatically and is retained across sessions.
๐ Reactive: Triggers re-renders like
useState
oruseGlobalState
.
๐ useGlobalState
vs. useProjectState
useGlobalState
vs. useProjectState
Feature
useGlobalState
useProjectState
Scope
All projects
Current project only
Persistence
Yes (cross-project)
Yes (per-project)
Use Case
Global settings, shared logic
Project-specific counters, configs
๐ง Best Practices
Use
useProjectState
for:Per-project configurations
Project-specific UI states (like tabs, toggles)
Data that shouldnโt affect other projects
Avoid using it for:
Data that must persist across all projects (use
useGlobalState
instead)
๐ Summary
useProjectState is the go-to hook when you need persistent, scoped state for a specific project in Nexo Maker. Just remember to provide a unique key:
And enjoy the flexibility of modular, project-aware components in your expansions!
Last updated