An open API service indexing awesome lists of open source software.

https://github.com/wkornewald/ts-react-struct

TypeScript package for using React and immutable.js with type-safe cursors and a central event system (as a Redux alternative)
https://github.com/wkornewald/ts-react-struct

cursor cursors events immutable immutablejs react redux typescript

Last synced: 2 months ago
JSON representation

TypeScript package for using React and immutable.js with type-safe cursors and a central event system (as a Redux alternative)

Awesome Lists containing this project

README

        

# ts-react-struct

[![npm version](https://badge.fury.io/js/ts-react-struct.svg)](https://badge.fury.io/js/ts-react-struct)
[![NPM downloads](https://img.shields.io/npm/dm/ts-react-struct.svg?style=flat)](https://npmjs.org/package/ts-react-struct)
[![Build Status](https://travis-ci.org/wkornewald/ts-react-struct.svg?branch=master)](https://travis-ci.org/wkornewald/ts-react-struct)

`ts-react-struct` is a TypeScript package for using `React` and `immutable.js` with type-safe cursors and a central event system.
The emphasis here is on type-safety.

You'll usually want to combine this with [ts-immutable-struct](https://github.com/wkornewald/ts-immutable-struct).

## Getting started

Install the package:
```sh
npm install --save ts-immutable-struct ts-react-struct
```

Example usage:
```typescript
import * as React from 'react'
import {ChangeEvent} from 'react'
import * as ReactDom from 'react-dom'
import {Struct, LeafRef} from 'ts-immutable-struct'
import {Component} from 'ts-react-struct'

interface Props extends React.HTMLProps {
valueRef: LeafRef,
}

class Input extends Component {
onChange = (event: ChangeEvent) => {
this.props.valueRef.val(event.target.value,event)
}

render() {
let props: Props = Object.assign({}, this.props)
delete props.valueRef
return
}
}

let data = Struct({
value: 'ahoy',
})

function render() {
return ReactDom.render(, document.getElementById('content'))
}
render()

data.observe((event, oldVal, newVal) => {
render()
})
```

Now, the state is always in sync with the DOM:
```typescript
data.get('value').deref()
// => "ahoy"

data.get('value').val('hola')
// => Input gets rerendered as

// <= user types "hey" into input field
data.get('value').deref() // => "hey"
```