Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

mobx-react-lite

Package Overview
Dependencies
Maintainers
2
Versions
78
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mobx-react-lite

Lightweight React bindings for MobX based on React 16.8+ and Hooks

  • 3.4.3
  • Source
  • npm
  • Socket score

Version published
Maintainers
2
Created

What is mobx-react-lite?

mobx-react-lite is a lightweight wrapper around MobX that provides React bindings for functional components. It allows you to use MobX for state management in your React applications with minimal boilerplate and high performance.

What are mobx-react-lite's main functionalities?

Observer Component

The `observer` function is used to create a reactive component that will automatically re-render when the observed state changes. In this example, the `Counter` component re-renders whenever `counter.count` is updated.

import React from 'react';
import { observer } from 'mobx-react-lite';
import { observable } from 'mobx';

const counter = observable({ count: 0 });

const Counter = observer(() => (
  <div>
    <button onClick={() => counter.count++}>Increment</button>
    <p>{counter.count}</p>
  </div>
));

export default Counter;

Using MobX stores

This example demonstrates how to use MobX stores with `mobx-react-lite`. The `CounterStore` class is an observable store, and the `Counter` component observes changes to the store and re-renders accordingly.

import React from 'react';
import { observer } from 'mobx-react-lite';
import { observable } from 'mobx';

class CounterStore {
  @observable count = 0;

  increment() {
    this.count++;
  }
}

const counterStore = new CounterStore();

const Counter = observer(() => (
  <div>
    <button onClick={() => counterStore.increment()}>Increment</button>
    <p>{counterStore.count}</p>
  </div>
));

export default Counter;

Using hooks with MobX

The `useLocalObservable` hook allows you to create local observable state within a functional component. This example shows how to use the hook to create a local counter state and update it within the component.

import React from 'react';
import { observer, useLocalObservable } from 'mobx-react-lite';

const Counter = observer(() => {
  const counter = useLocalObservable(() => ({ count: 0, increment() { this.count++; } }));

  return (
    <div>
      <button onClick={counter.increment}>Increment</button>
      <p>{counter.count}</p>
    </div>
  );
});

export default Counter;

Other packages similar to mobx-react-lite

Keywords

FAQs

Package last updated on 08 Mar 2023

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