Nexo Maker Docs
  • Welcome
  • Getting Started
    • Basic Concepts
      • Starting a Expansion
      • Compiling your Expansion
  • Visual API
    • Modularjs
      • Modularjs Basics
      • Styling
      • useState()
      • useEffect()
      • useGlobalState()
      • useProjectState()
    • [Deprecated] Components
      • Custom HTML documents
    • Themes
      • Replace Assets
      • Assets List
  • Functional API
    • Post Functions
      • Post Sidebar Button
      • Post Editor Module
    • Get Functions
      • Load Assets
      • Load File
      • Get Project
  • API Changelogs
    • Changelog 0.6.1
Powered by GitBook
On this page
  • ๐Ÿ—‚๏ธ Using useProjectState in Modularjs
  • ๐Ÿ’ก What Is useProjectState?
  • ๐Ÿงช Example: Project-Specific Counter
  • ๐Ÿ“Œ Syntax
  • โœ… Why Use useProjectState?
  • ๐Ÿ†š useGlobalState vs. useProjectState
  • ๐Ÿง  Best Practices
  • ๐Ÿš€ Summary
  1. Visual API
  2. Modularjs

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

module.exports = ({ useProjectState }) => {
  const [count, setCount] = useProjectState('countz', 0);

  return (
    <div>
      <h1>Project Counter</h1>
      <p>Countz: {count}</p>
      <button onClick={() => setCount(countz + 1)}>Increment</button>
    </div>
  );
};

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

const [state, setState] = useProjectState('unique-key', defaultValue);
  • ๐Ÿ†” '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 or useGlobalState.


๐Ÿ†š 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:

const [value, setValue] = useProjectState('my-key', initialValue);

And enjoy the flexibility of modular, project-aware components in your expansions!

PrevioususeGlobalState()Next[Deprecated] Components

Last updated 16 days ago