Giter Site home page Giter Site logo

use-interpolate's Introduction

use-interpolate ๐Ÿ“ƒ

A react hook that interpolates markup tags into components

npm license

Install

npm i use-interpolate

Example

import useInterpolate from 'use-interpolate'

function App () {
    const render = useInterpolate('<wrap>My name is <name /> and I am <age /> years old.</wrap>')

    const components = {
        wrap: children => (<Content>{children}</Content>),
        name: <input type="text" />,
        age: <input type="number" />
    }

    return <div>{render(components)}</div>
}
<div>
    <Content>
        My name is <input type="text" /> and I am <input type="number" /> years old.
    </Content>
</div>

API

useInterpolate(string, options?)

This hook parses the given string and returns a render function that interpolates markup tags into components.

options

prefix

This is a tag prefix option. The default is <.

suffix

This is a tag suffix option. The default is >.

strict

This is a strict option to parse. The default is true.

render(components?)

This function interpolates markup tags to the components from the parsed result.

const render = useInterpolate('hello <0 /> word')

return <div>{render({ 0: <br/> })}</div> // => <div>hello <br /> word</div>

There is a way to interpolate using functions.

const render = useInterpolate('<0>hello word</0>')

return render({ 0: children => (<p>{children}</p>) }) // => <p>hello word</p>

License

MIT

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.