Socket
Socket
Sign inDemoInstall

mobx-react

Package Overview
Dependencies
Maintainers
4
Versions
144
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.


Version published
Weekly downloads
1.1M
decreased by-2.18%
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

FAQs

Package last updated on 06 Nov 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