JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 1
  • Score
    100M100P100Q33552F
  • License MIT

A react hook that interpolates tag into a component.

Package Exports

  • use-interpolate

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (use-interpolate) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

use-interpolate 📃

A react hook that interpolates tag into a component.

npm typescript license

Install

npm i use-interpolate

Example

import useInterpolate from 'use-interpolate'

function App(){
    const vnode = useInterpolate(
        '<0>name</0><1><2/></1>',
        {
            0: <span/>,
            1: (children) => <p> - {children}</p>,
            2: <Input theme='blue'/>,
        }
    )
    return <div>{vnode}</div>
}

output:

<div>
    <span>name</span><p> - <Input theme='blue'/></p>
</div>

API

useInterpolate(text, components)

A react hook that interpolates tag into a component.

createHook(options)

Create a custom interpolate hook.

options

tag

change tag bracket.

import { createHook } from 'use-interpolate'

const useInterpolate = createHook({ tag: ['{', '}'] })

function App(){
    const vnode = useInterpolate(
        'hello {0}world{/0}',
        {
            0: <span/>
        }
    )
    return <div>{vnode}</div>
}

output:

<div>
    hello <span>world</span>
</div>
strict

Default is true. If false, no error occurs.

const useInterpolate = createHook({ strict: false })

function App(){
    const vnode = useInterpolate('<a>Invalid<b>', {a: <p/>, b: <br/>}) // no error.
    /* ... */
}

output:

<p>Invalid<br/></p>

interpolate(text, components)

In a class component, an interpolation function is used instead of a hook. However, this does not support caching for parsed text.

import { interpolate } from 'use-interpolate'

class App extends React.Component{
    render(){
        const vnode = interpolate(`hello <0/> john!`, {0: <br/>})
        /* ... */
    }
}

createInterpolate(options)

Create a custom interpolate function.

import { createInterpolate } from 'use-interpolate'

const interpolate = createInterpolate({ strict:false, tag: ['{', '}'] })

options

Same as createHook.

  • tag-name-parser - A tag parser that does not support attributes. Lightweight and fast.

License

MIT