UNPKG

react-string-template

Version:

react-string-template is simple string template with react

62 lines (45 loc) 1.28 kB
# react-string-template react-string-template is simple string template with react ## install `npm i --save react-string-template` ``` jsx <ReactStringTemplate str={"{name} is a series of {foo} novels written by British author {author} in {yeah}."} renderNoMatch={()=>'not match'} values={{ name: <a href="https://www.google.com/search?q=Harry Potter" key='potter'>Harry Potter</a>, // Don't forget write key foo: 'fantasy', bar: 'novels', author: 'J. K. Rowling', yeah: 1997, }} > {(children)=><div>{children}</div>} </ReactStringTemplate> //Harry Potter is a series of fantasy novels written by British {author} J. K. Rowling in 1997. ``` ``` jsx // add click event <ReactStringTemplate str={"{charmName} Charm"} values={{ charmName: ( <span key='patronus' onClick={()=>alert('Patronus!')}> Patronus </span> ), }} > {(children)=><div>{children}</div>} </ReactStringTemplate> ``` ``` jsx // render no Match <ReactStringTemplate str={"I am {name}"} renderNoMatch={()=>'not match'} > {(children)=><div>{children}</div>} </ReactStringTemplate> //I am not match ``` If you don't need escape use `{{escape}}`