You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

mobx-react

Package Overview
Dependencies
Maintainers
4
Versions
145
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mobx-react

React bindings for MobX. Create fully reactive components.

9.2.0
latest
Source
npmnpm
Version published
Weekly downloads
1.3M
-0.19%
Maintainers
4
Weekly downloads
 
Created

What is mobx-react?

The mobx-react package provides React bindings for MobX, a state management library. It allows you to connect your React components to MobX observables, making it easier to manage and react to state changes in your application.

What are mobx-react'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.

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

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

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

export default Counter;

Injecting Stores

The inject function is used to inject stores into components, making it easier to access MobX stores within your React components.

import React from 'react';
import { Provider, inject, observer } from 'mobx-react';
import { observable } from 'mobx';

class Store {
  @observable count = 0;
}

const store = new Store();

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

const App = () => (
  <Provider store={store}>
    <Counter />
  </Provider>
);

export default App;

Using MobX with React Hooks

The useLocalObservable hook is used to create local observable state within a functional component, allowing you to use MobX with React hooks.

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

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

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

export default Counter;

Other packages similar to mobx-react

Keywords

mobx

FAQs

Package last updated on 11 Dec 2024

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