Skip to content

alshdavid-graveyard/use-view-model

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

React View Model Hook

Add Angular/Vue-like Reactivity to React

import React from "react"
import { useViewModel } from 'use-view-model'

export class FooViewModel {
  title = 'Foobar'
}

export const Foo = () => {
  const vm = useViewModel(() => new FooViewModel())

  return <div>
    <div>{vm.title}</div>
    <input 
      type="text" 
      value={vm.title}
      onChange={e => vm.title = e.target.value} />
  </div>
}

Methods

You can add methods to your view model

import React from "react"
import { useViewModel } from 'use-view-model'

export class BarViewModel {
  title = 'Foobar'

  reset() {
    this.title = 'Foobar'
  }
}

export const Bar = () => {
  const vm = useViewModel(() => new BarViewModel())

  return <div>
    <div>{vm.title}</div>
    <input 
      type="text" 
      value={vm.title}
      onChange={e => vm.title = e.target.value} />
    <button
      onClick={() => vm.reset()}>
      Reset
    </button>
  </div>
}

Testing

You can test view logic without being concerned about the DOM or browser. You can take also advantage of dependency injection.

import { BarViewModel } from './foo.tsx'

it('Should reset title when', () => {
  const vm = new BarViewModel()
  const startingValue = vm.title
  vm.title = 'changed'
  
  vm.reset()

  expect(vm.title).toBe(startingValue)
})

Preact

To use this for Preact change the import path to

import { useViewModel } from 'use-view-model/preact'

About

Angular like reactivity in React

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published