NestedLink API reference

Links can be created and used inside of the React component.render() method.

Linking to state with React Hook

useLink hook creates the component's state variable wrapped in a hook.

import { useLink } from 'valuelink'
import * as React from 'react'

export const MyCoolComponent = ( props ) => {
    const name = useLink( '' );

    return (
        <input {...name.props} />
    )
}

Extracts link values.

export const MyCoolComponent = ( props ) => {
    const name = useLink( 'a' ),
          email = useLink( 'b' ),

    ...
    const values = linksValues({ name, email });
    console.log( values ); // { name : 'a', email : 'b' }
}

Extracts link validation errors. Returns an empty object if there are no errors.

export const MyCoolComponent = ( props ) => {
    const name = useLink( 'a' ),
          email = useLink( 'b' ),

    ...
    const values = linksErrors({ name, email });
    console.log( values ); // { name : 'a', email : 'b' }
}

Bulk set links from the object with values.

export const MyCoolComponent = ( props ) => {
    const name = useLink( 'a' ),
          email = useLink( 'b' ),

    ...
    // Somewhere on I/O completion:
    setLinks({ name, email }, json);
}

Linking to the state attributes

Create link to an attribute of the component's state. Component must extend LinkedComponent class.

Can be overriden to create custom data binding for something different than the React state.

const nameLink = this.linkAt( 'name' ),
      emailLink = this.linkAt( 'email' );

Create an object with links to listed state members. When no keys are provided, it creates link to every member of the state. Component must extend LinkedComponent class.

Can be overriden to create custom data binding for something different than the React state.

const links = this.linkAll( 'name', 'email' ),
      { name, email } = links;

Same as component.linkAt(), but works with any component class.

const nameLink = Link.state( this, 'name' ),
      emailLink = Link.state( this, 'email' );

Same as component.linkAll(), but works with any component class.

const links = Link.all( this, 'name', 'email' ),
      { name, email } = links;

All links created during render() are cached inside the component.link object. Direct access to the cache may be used in event handlers to reference these links.

When you create the link to the value which has not been changed since the last render, link object will be reused. Which means that it's safe to use pure render optimization.

Create link to the member of array or object.

If linked value is plain object or array, it's possible to generate links to their members. Whenever this derivative links will be updated, it will lead to proper purely functional update (with shallow copying) of the parent element.

const deepLink = this.linkAt( 'array' ).at( 0 ).at( 'name' );
deepLink.set( 'Joe' ); // Will update component state.array

Create links to the object's members, and wrap them in an object.

const links = userLink.pick( 'name', 'email' ),
      { name, email } = links;
![method] linkToObject.map( ( linkToItem, itemKey ) => any | void ) : any[]

Map and filter through array or object.

var list = stringArrayLink.map( ( itemLink, index ) => {
    if( itemLink.value ){ // Skip empty elements
        return (
            <div key={ index }>
                <Input valueLink={ itemLink } />
            </div>
        );
    }
});

Bind to control

![var] link.props : { value, onChange }

Bind link to the standard form control consuming value and onChange props.

<input {...link.props} />

Custom data-bound controls

You're encouraged to create your own semantic form controls to take the full advantage of the value links features. An example of the control:

const Input = ({ valueLink, ...props }) => (
    <div className={`form-control ${ valueLink.error ? 'error' : '' }`}>
        <input {...props}
            value={ valueLink.value }
            onChange={ e => valueLink.set( e.target.value ) }
        />
        <div className="validation-error">{ valueLink.error || '' }</div>
    </div>
);

Creates the link to the presence of value in array.

Resulting link value is true whenever element is present in array, and false otherwise. Whenever resulting link is assigned with new value, it will flip element in the array.

Useful for the large checkbox groups.

const optionXBoolLink = arrayLink.contains( 'optionX' );

Create boolean link to value equality.

Resulting link value is true whenever parent link value equals to whenTrue, and false otherwise. When resulting link is assigned with true, it sets parent link value with whenTrue, and with null otherwise.

Useful for radio groups.

const optionXLink = stringLink.equals( 'optionX' );

Create boolean link which value is false when parent link is null (or undefined), and true otherwise. Whenever the enabled-link is set to true, it sets parent link to the defaultValue.

This type of links is used to support enabling/disabling of individual form controls with a dedicated checkbox. <Input> control and the rest of form controls must be modified to disable themselves when its valueLink.value === null.

const textLink = this.linkAt( 'text' );

return (
    <Checkbox checkedLink={ textLink.enabled() } />
    <Input valueLink={ textLink } /> 
);

Create custom link with the given value and update function.

It may be used for different scenarios. Good example is to use 'edit element' component for adding new element.

Imagine that we have a component <EditUser valueLink={ userLink } /> expecting the link to an object. When editing is finished, EditUser will update the given link with a new values.

Then, following custom link will allow you to add new user with the same form element.

<EditUser valueLink={ Link.value( {}, x => userArrayLink.push( x ) ) } />

Read more about links to objects updates in the next section.

Create the wrapper for existing link which will invoke callback whenever new value is set. Similar to:

Link.value( link.value, x => {
    callback( x );
    link.set( x );
});

Create the wrapper for existing link which will invoke given transform function before new value is set. Returned value will be used as new link value, and if it's undefined update will be rejected. Similar to:

Link.value( link.value, x => {
    const y = callback( x, link.value );
    if( y !== undefined ){
        link.set( y );
    }
});

Usage example:

<Input valueLink={ strLink.pipe( x => x && x.toUpperCase() ) }/>

Note for TypeScript users

Link is the parametric type Link< T >, where T is the type of the enclosed value.

TypeScript properly infers type of the link and perform static checks failing on missing state members.

interface MyState {
    name : string
}
...
const nameLink = this.linkAt( 'name' ); // Link< string >
const missingLink = this.linkAt( 'age' ); // Compile-time error - no such a member in state.

Simple value updates

![method] link.set( x ) : void
![method] link.requestChange( x ) : void

Set link to the given value.

<button onClick={ () => boolLink.set( !boolLink.value ) } />
![method] link.update( prevValue => any ) : void

Update link value using the given value transform function.

<button onClick={ () => boolLink.update( x => !x ) } />
![method] link.action( ( prevValue, event ) => any ) : ( event => void )

Create UI event handler which will transform the link.

link.action takes transform function, and produce a new function which takes single event argument. When it's called, event and link value are passes as transform parameters, and link will be updated with returned value.

This is particularly useful in (but not restricted to) UI event handlers.

// simple click event handler...
<button onClick={ boolLink.action( x => !x ) } />

// manual binding to input control:
const setValue = ( x, e ) => e.target.value;
...
<input  value={ link.value }
        onChange={ link.action( setValue ) } />

Plain objects and arrays are shallow copied by link.update() and within link.action() handlers, thus it's safe just to update the value in place.

![method] linkToObject.update( clonedObject => Object ) : void

Update enclosed object or array.

![method] linkToObject.action( ( clonedObject, event ) => Object ) : ( event => void )

Creates action to update enclosed object or array.

<button onClick={ () => objLink.update( obj => {
                                obj.a = 1;
                                return obj;
                            }) } />
![method] linkToObject.removeAt( key ) : void
![method] linkToObject.at( key ).remove() : void

Remove element with a given key from the enclosed object ar array.

Link to arrays proxies some important Array methods.

![method] linkToArray.splice( ... ) : void
![method] linkToArray.push( ... ) : void
![method] linkToArray.unshift( ... ) : void

Works in the same way and accepts the same parameters as corresponding Array method, but returns undefined and leads to the proper purely functional update of the parent object chain.

It's highly recommended to read tutorial on validation with value links.

Evaluate given condition for the current link value, and assign given error object to the link.error when it fails. There are no restriction on the error object shape and type.

It's possible to assign default error message to the validator function. tags.jsx provides isRequired and isEmail generic validator functions as an examples. Excerpt from tags.jsx:

export const isRequired = x => x != null && x !== '';
isRequired.error = 'Required';

Checks can be chained. In this case, the first check which fails will leave its error in the link.

![var] link.error : any | void

This link field may be analyzed by custom <Input /> control to indicate an error (see tags.jsx controls and supplied examples).

This mechanics can be used to add ad-hoc validation in render.

// Simple check
const numLink = List.state( this, 'num' )
                .check( x => x >= 0 && x <=5 );

console.log( numLink.error );

// Check with error message
const numLink = List.state( this, 'num' )
                .check( x => x >= 0 && x <=5, 'Number must be between 0 and 5' );

console.log( numLink.error );

// Chained checks
const numLink = List.state( this, 'num' )
                .check( x => x >= 0, 'Negative numbers are not allowed' )
                .check( x => x <= 5, 'Number should be not greater than 5' );

console.log( numLink.error );