# use-dotnetify

> React Hook for pub-sub behavior using dotNetify.

Latest version **2.0.0** (published 2019-08-17) · MIT license · 0 weekly downloads

## Install

```sh
npm install use-dotnetify
pnpm add use-dotnetify
yarn add use-dotnetify
bun add use-dotnetify
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.0 |
| Published | 2019-08-17 |
| First published | 2018-11-16 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 18.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 4 |
| Author | Aaron Roberts |
| Maintainers | lilasquared |

## Links

- npm: https://www.npmjs.com/package/use-dotnetify
- Repository: https://github.com/lilasquared/use-dotnetify
- Homepage: https://github.com/lilasquared/use-dotnetify#readme
- Issues: https://github.com/lilasquared/use-dotnetify/issues
- npm.io page: https://npm.io/package/use-dotnetify

## Dependencies (1)

- [use-force-update](https://npm.io/package/use-force-update.md) ^1.0.7

## Recent versions

- 2.0.0 (latest) — 2019-08-17
- 1.0.0 — 2018-11-16

## README

# use-dotnetify

`use-dotnetify` is a react library that provides hook access for [`dotnetify`](http://dotnetify.net/core/overview) models.

## Install

You must be using [`dotnetify`](http://dotnetify.net/core/overview).

- `npm install use-dotnetify` or
- `yarn add use-dotnetify`

## Use

Using this library requires two parts. First is the Provider which uses react context to share the states and vms through the component hierarchy.

```js
import React from "react"
import { DotNetifyProvider } from "use-dotnetify"
import HelloWorld from "./HelloWorld"

const App = () => (
  return (
    <DotnetifyProvider>
      <HelloWorld />
    </DotnetifyProvider>
  )
)
```

Now our HelloWorld component can access the view models and state provided by the server through the `useDotNetify()` hook. This will give us access to the state and vm objects generated by `dotnetify.react.connect()`.

```jsx
import React from "react"
import { useDotNetify } from "use-dotnetify"

const initialState = {
  FirstName: "",
  LastName: "",
  FullName: "",
}

const HelloWorld = () => {
  const [state, vm] = useDotNetify("HelloWorldVm", initialState)

  return (
    <div>
      <strong>{state.FullName}</strong>
    </div>
  )
}
```

Also provided is a simple hook that wraps state dispatches for single properties. This hook is `useProperty()` and accepts the model name and property name as parameters.

```jsx
import React from "react"
import { useProperty } from "use-dotnetify"

const HelloWorld = () => {
  const [firstName, setFirstName, dispatchFirstName] = useProperty("HelloWorldVm", "FirstName")
  const [lastName, setLastName, dispatchLastName] = useProperty("HelloWorldVm", "LastName")

  return (
    <div>
      <input
        value={firstName}
        onChange={e => setFirstName(e.target.value)}
        onBlur={e => dispatchFirstName(e.target.value)}
      />
      <br />
      <input
        value={lastName}
        onChange={e => setLastName(e.target.value)}
        onBlur={e => dispatchLastName(e.target.value)}
      />
    </div>
  )
}
```

In the example above `firstName` and `setFirstName` function exactly as `useState()` would, only maintaining local state. The third returned value `dispatchFirstName()` will dispatch an update to the server that will update the `HelloWorldVm.FirstName` property with the new value.

---
_Source: https://npm.io/package/use-dotnetify · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
