Permalink
498b01e Jan 31, 2018
1 contributor

Users who have contributed to this file

104 lines (67 sloc) 1.48 KB

<List>

Gives you an array as a state.

Usage

import {List} from 'libreact/lib/List';

<List init={[1, 2, 3]}>{({value, set, push, filter, sort}) =>
  <div>
    <pre style={{fontFamily: 'monospace'}}>{JSON.stringify(value)}</pre>
    <button onClick={() => push(0)}>push</button>
    <button onClick={() => filter((i) => i > 0)}>filter</button>
    <button onClick={() => sort()}>sort</button>
  </div>
}</List>

Props

Signature

interface IListProps {
  init?: any[];
}

, where

  • init - optional, array of any values.

withList() HOC

HOC that merges list prop into enhanced component's props.

import {withList} from 'libreact/lib/List';

const MyCompWithList = withList(MyComp);

You can overwrite the injected prop name

const MyCompWithList = withList(MyComp, 'foobar');

Or simply merge the whole object into your props

const MyCompWithList = withList(MyComp, '');

Set default value

const MyCompWithList = withList(MyComp, '', [1, 2, 3]);

@withList decorator

React stateful component decorator that adds list prop.

import {withList} from 'libreact/lib/List';

@withList
class MyComp extends Component {

}

Specify different prop name

@withList('foobar')
class MyComp extends Component {

}

or merge the the whole object into props

@withList('')
class MyComp extends Component {

}

set starting value

@withList('', [1, 2, 3])
class MyComp extends Component {

}