Security News
Input Validation Vulnerabilities Dominate MITRE's 2024 CWE Top 25 List
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
doura-react
Advanced tools
Doura-react includes its own hook APIs, which allow your React Components to subscribe to the doura store and dispatch actions.
Install with npm:
npm install doura-react
Install with yarn
yarn add doura-react
Doura offers multiple hooks for different situations.
useModel
would be the first choice if there is no suitable hooks to choose.
useRootModel
aims to store global variable that offers for all react components.
useRootStatic
aims to store global static data, such as environment variable, only get it at first time.
useSharedModel
allows you to share data across specific components, not globally but locally.
useStaticModel
allows you to share local static data across specific components.
<DouraRoot />
component, which makes the state shared by the rest of your app:// src/index.ts
import * as React from 'react'
import ReactDOM from 'react-dom'
import { DouraRoot } from 'doura-react'
import App from './App'
function App() {
return ReactDOM.render(
<DouraRoot>
<App />
</DouraRoot>,
document.getElementById('root')
)
}
// src/model.ts
export const user = defineModel({
name: 'user',
state: { name: 'alice', isLogin: false },
actions: {
login() {
this.login = true
},
},
})
// src/App.tsx
import * as React from 'react'
import {
ISelectorParams,
createContainer,
useRootModel,
} from 'doura-react'
import { user } from './model'
const selector = function (data: ISelectorParams<typeof user>) {
return data.isLogin
}
export default function App() {
const [isLogin, { login }] = useRootModel(user, selector)
return isLogin ? <div>welcome</div> : <div onClick={login}>login</div>
}
import * as React from 'react';
import ReactDOM from 'react-dom';
import { createContainer } from 'doura-react';
const { Provider: LocalProvider, useSharedModel, useStaticModel } = createContainer();
function ComponentA() {
const [state, actions] = usesharedModel(user);
const [staticState, _] = useStaticModel(user);
return (
...
)
}
function ComponentB() {
const [state, actions] = usesharedModel(user);
const [staticState, _] = useStaticModel(user);
return (
...
)
}
export default function ComponentWithSharedModel() {
return (
<LocalProvider>
<ComponentA />
<ComponentB />
</LocalProvider>
)
}
We can replace all useState
, useReducer
, useMemo
and useCallback
calls by using useModel
.
import { useModel } from 'doura-react'
function Counter() {
const [state, actions] = useModel({
state: {
count: 0,
},
actions: {
increment() {
this.count += 1
},
decrement() {
this.count -= 1
},
},
})
return (
<>
Count: {state.count}
<button onClick={actions.decrement}>-</button>
<button onClick={actions.increment}>+</button>
</>
)
}
import { useModel } from 'doura-react';
const [state, actions] = useModel(model: IModel, selector?: ISelector);
Most of time you would use useModel
to extract data from the model. It will create new context everytime you use it.
import { useRooteModel } from 'doura-react';
const [state, actions] = useRooteModel(model: IModel, selector?: ISelector);
Global Provider
context, you can get the global context anywhere, even if the component is unmount, the state will not be destroyed.
import { useRootStaticModel } from 'doura-react';
const [state, actions] = useRootStaticModel(model: IModel, selector?: ISelector);
useRootStaticModel
is similar to useRootModel
, the different is that, useRootStaticModel
will not rerender on state changed.
It returns a independent scope Provider, useSharedModel, useStaticModel
for context and methods to consume models.
import { createContainer } from 'doura-react'
export const { Provider, useSharedModel, useStaticModel } = createContainer()
In the same Provider
context, the state is shared across the store
const [state, actions] = useSharedModel(model: IModel, selector?: ISelector);
Share the context across components
const [state, actions] = useStaticModel(model: IModel, selector?: ISelector);
useStaticModel
is similar to useSharedModel
, the different is that, useStaticModel
will not rerender on state changed.
useStaticModel
doest not support Destructuring Assignment.
import React from 'react'
import ReactDOM from 'react-dom'
import App from './App'
import { doura } from 'doura'
import douraLog from 'doura-log'
import persist, { createWebStorage } from 'doura-persist'
import { DouraRoot } from 'doura-react'
const modelManager = doura({
initialState: {},
plugins: [
[douraLog],
[
persist,
{
key: 'root',
storage: createWebStorage('local'),
// whitelist: ['b'],
blacklist: ['b'],
migrate: function (storageState: any, version: number) {
const count = storageState.count
if (count && count.value >= 3) {
count.value = 2
}
return storageState
},
},
],
],
})
ReactDOM.render(
<DouraRoot modelManager={modelManager}>
<App />
</DouraRoot>,
document.getElementById('root')
)
FAQs
Rematch core with react hooks
The npm package doura-react receives a total of 3 weekly downloads. As such, doura-react popularity was classified as not popular.
We found that doura-react demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.
Research
Security News
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.