react-pizza
Version:
The utility for create (p)react component habitat.
93 lines (69 loc) • 2.82 kB
Markdown
[](https://travis-ci.org/imcuttle/react-pizza)
[](https://codecov.io/github/imcuttle/react-pizza?branch=master)
[](https://www.npmjs.com/package/react-pizza)
[](https://www.npmjs.com/package/react-pizza)
[](https://prettier.io/)
[](https://conventionalcommits.org)
> The utility for creating (p)react component habitat.
## Feature
- Support react and [preact](https://github.com/developit/preact)
- Set props and call method or get attribute which [preact-habitat](https://github.com/zouhir/preact-habitat) is not supported
## Installation
```bash
npm install react-pizza
# or use yarn
yarn add react-pizza
```
## Usage
```javascript
import * as React from 'react'
import * as pizza from 'react-pizza'
class Timer extends React.Component {
state = {
count: this.props.count
}
increase() {
this.setState({ count: this.state.count + 1 })
}
render() {
return <p>{this.state.count}</p>
}
}
// document.body
// <div id="root"></div>
const render = pizza(Timer)
const timer = render('#root', { count: 10 })
// <div id="root"><p>10</p></div>
timer.call('increase') // Call `increase` method
// <div id="root"><p>11</p></div>
timer.ref(0).current.state === timer.ref()[0].current.state) === timer.call('state', 0)
// { count: 11 }
// Set the props of all elements
timer.setProps({ count: 10 })
// Set the prop of #root element
timer.setProps({ count: 10 }, document.querySelect('#root'))
// Set the prop of first element
timer.setProps({ count: 10 }, 0)
// Unmount timer in all elements
timer.remove()
timer.remove(0)
timer.remove(document.querySelect('#root'))
// Mount origin
timer.origin === render.origin
timer.origin === Timer
```
- Fork it!
- Create your new branch:
`git checkout -b feature-new` or `git checkout -b fix-which-bug`
- Start your magic work now
- Make sure npm test passes
- Commit your changes:
`git commit -am 'feat: some description (close #123)'` or `git commit -am 'fix: some description (fix #123)'`
- Push to the branch: `git push`
- Submit a pull request :)
This library is written and maintained by imcuttle, <a href="mailto:moyuyc95@gmail.com">moyuyc95@gmail.com</a>.
MIT - [imcuttle](https://github.com/imcuttle) 🐟