LogoLogo
  • Introduction
  • Intro
    • 🚀Getting Started
    • Key Characteristics
    • Fundamentals
    • Motivation
  • Fundamentals
    • 💾Units
      • BoolUnit
      • NumUnit
      • StringUnit
      • DictUnit
      • ListUnit
      • GenericUnit
    • 🤝Systems
      • AsyncSystem
      • Custom AsyncSystem
    • 🤜Action
    • 📦Cluster
  • 🔨Utilities
    • Stream
    • Selection
  • Integrations
    • Angular
    • React
      • useObservable Hook
      • useUnit Hook
  • 📖Guides
    • Configuration
    • Nesting
    • Events
    • Typings
    • Caching
    • Persistence
    • Immutability
    • Freeze and Mute
    • Development Environment
    • General Guidelines
  • More
    • 👀Examples
    • ✍️Articles
Powered by GitBook
On this page
  • Examples:
  • 1. Counter using Hooks
  • 2. Counter using useUnit Hook
  • 3. Counter using useObservable Hook

Was this helpful?

  1. Integrations

React

PreviousAngularNextuseObservable Hook

Last updated 4 years ago

Was this helpful?

Using ActiveJS with React is pretty straightforward since ActiveJS has the same basic API as every other subscription-based State Management solution. If you're following the , then you are already on the right path.

Apart from the following examples, you will find more examples in the section.

Examples:

1. Counter using Hooks

Here's an example of implementing a simple counter with ActiveJS and React Hooks.

// initialize a NumUnit.
const counterUnit = new NumUnit() // with default value 0

// define two pure functions that produce a new value
const increment = value => value + 1;
const decrement = value => value - 1;

// create a react component
function App() {
  const [value, setValue] = useState<number>(); // create local state

  // use an effect to update the local state when counterUnit's value changes
  useEffect(() => {
    const subscription = counterUnit.subscribe(setValue);
    return () => subscription.unsubscribe(); // prevent memory leak
  }, []);

  return (
    <React.Fragment>
      Counter Value: {value}

      <button onClick={() => counterUnit.dispatch(increment)}>Increase</button>
      <button onClick={() => counterUnit.dispatch(decrement)}>Decrease</button>
    </React.Fragment>
  );
}
// initialize a NumUnit.
const counterUnit = new NumUnit() // with default value 0

// define two pure functions that produce a new value
const increment = value => value + 1
const decrement = value => value - 1

// create a react component
function App() {
  const {value, dispatch, resetValue} = useUnit(counterUnit)

  return (
    <React.Fragment>
      Counter Value: {value}

      <button onClick={() => dispatch(increment)}>Increase</button>
      <button onClick={() => dispatch(decrement)}>Decrease</button>
      <button onClick={() => resetValue()}>Reset</button>
    </React.Fragment>
  )
}
// initialize a NumUnit.
const counterUnit = new NumUnit() // with default value 0

// define two pure functions that produce a new value
const increment = value => value + 1;
const decrement = value => value - 1;

// create a react component
function App() {
  const value = useObservable(counterUnit);

  return (
    <React.Fragment>
      <p>
        Counter Value: <b>{value}</b>
      </p>

      <button onClick={() => counterUnit.dispatch(increment)}>Increase</button>
      <button onClick={() => counterUnit.dispatch(decrement)}>Decrease</button>
      <button onClick={() => counterUnit.resetValue()}>Reset</button>
    </React.Fragment>
  );
}

Try it out on .

2. Counter using Hook

Here's the same counter example using the Hook. This is much better than replicating the same useState and useEffect logic every time you want to use a Unit.

Try it out on .

3. Counter using Hook

Here's the same counter example again, using the Hook. This is even better than , since we don't have to re-map the Unit's API again in a Hook, we just need the dynamic value to trigger rendering update, and for dispatch and other methods, we can use the Unit directly.

Try it out on .

general guidelines
Examples
NumUnit
StackBlitz
useUnit
useUnit
StackBlitz
useObservable
useObservable
useUnit
StackBlitz