
Security News
Security Community Slams MIT-linked Report Claiming AI Powers 80% of Ransomware
Experts push back on new claims about AI-driven ransomware, warning that hype and sponsored research are distorting how the threat is understood.
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.
useStaticModeldoest 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
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
Experts push back on new claims about AI-driven ransomware, warning that hype and sponsored research are distorting how the threat is understood.

Security News
Ruby's creator Matz assumes control of RubyGems and Bundler repositories while former maintainers agree to step back and transfer all rights to end the dispute.

Research
/Security News
Socket researchers found 10 typosquatted npm packages that auto-run on install, show fake CAPTCHAs, fingerprint by IP, and deploy a credential stealer.