Installations
npm install mobx-store-provider
Releases
Maintenance release
Published on 05 Jan 2022
Minor bug fix
Published on 23 Mar 2021
Added minor feature
Published on 22 Mar 2021
Maintenance release
Published on 20 Mar 2021
Maintenance release
Published on 21 Dec 2020
Missing env proxy for useCreateStore
Published on 29 Nov 2020
Developer
Developer Guide
Module System
CommonJS
Min. Node Version
Typescript Support
Yes
Node Version
14.17.0
NPM Version
7.16.0
Statistics
42 Stars
932 Commits
3 Forks
3 Watching
3 Branches
1 Contributors
Updated on 04 Jul 2024
Languages
TypeScript (100%)
Total Downloads
Cumulative downloads
Total Downloads
195,259
Last day
-19.4%
187
Compared to previous day
Last week
-29.8%
1,216
Compared to previous week
Last month
20.9%
6,948
Compared to previous month
Last year
36.2%
62,624
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Peer Dependencies
2
mobx-store-provider
mobx-store-provider is a library that provides React Hooks to setup and access mobx-state-tree models from within React Function Components.
Its goal is to provide a straight-forward, minimalist, and terse API that allows you to easily incorporate mobx-state-tree into functional React components.
- Installation
- Basic example
- API details and examples
- useProvider - Provide your components with a store
- useCreateStore - Create a new store inside a component
- useStore - Use a store in a component
- getStore - Use a store outside a component
- Multiple stores
- Local state
- Typescript
- Testing
- Motivation
- Upgrading 1.x -> 2.x
Installation
1npm i mobx-store-provider
1yarn add mobx-store-provider
Basic example
The following shows an example application using mobx-store-provider.
App component
At the core of the application we define the main App
component.
Inside of the App
we use hooks provided by mobx-store-provider to:
- Create the
appStore
instance with the useCreateStore hook - Retrieve the
Provider
with the useProvider hook
1// App.jsx (Main App component, we use it to create and provide the store) 2import React from "react"; 3import { useProvider, useCreateStore } from "mobx-store-provider"; 4import AppStore from "./AppStore"; 5import UserDisplay from "./UserDisplay"; 6 7function App() { 8 // Create the AppStore instance 9 const appStore = useCreateStore(AppStore, { user: "Jonathan" }); 10 11 // Get the Provider for the AppStore 12 const Provider = useProvider(AppStore); 13 14 // Wrap the application with the Provider passing it the appStore 15 return ( 16 <Provider value={appStore}> 17 <UserDisplay /> 18 </Provider> 19 ); 20} 21 22export default App;
Note that we wrap the application with the Provider
, supplying it the appStore
as its value.
This makes the appStore
available to the rest of the application.
Using the store
In another component somewhere in the application we want to use or gain access to the appStore
.
To do this, we use the useStore hook:
1// UserDisplay.jsx (A component, we use the store from above inside it) 2import React from "react"; 3import { observer } from "mobx-react"; 4import { useStore } from "mobx-store-provider"; 5import AppStore from "./AppStore"; 6 7function UserDisplay() { 8 // Get access to the store 9 const appStore = useStore(AppStore); 10 return <div>{appStore.user}</div>; 11} 12 13// Wrap it with mobx-react observer(), so updates get rendered 14export default observer(UserDisplay);
Note that we also wrap the component with observer()
from the mobx-react library.
This is critical, as it ensures the component will render any updates made to the appStore
. For more information, see the observer documentation for the mobx-react library.
Defining the store
The code above uses the AppStore
mobx-state-tree model. In the context of mobx-store-provider this is referred to as a store
.
1// AppStore.js (mobx-state-tree store/model) 2import { types } from "mobx-state-tree"; 3 4const AppStore = types.model({ 5 user: types.string, 6}); 7 8export default AppStore;
If you are new to mobx-state-tree, it is recommended you read through the mobx-state-tree documentation.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
7 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
- Warn: Project is vulnerable to: GHSA-p8p7-x288-28g6
- Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw
- Warn: Project is vulnerable to: GHSA-72xf-g2v4-qvf3
- Warn: Project is vulnerable to: GHSA-3h5v-q93c-6h6q
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
Found 0/18 approved changesets -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 12 are checked with a SAST tool
Score
2.1
/10
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More