1.0.0 • Published 5 years ago
react-nesta v1.0.0
react-nesta
连接 react 和 nesta 的工具库。
📦 安装
npm install react-nesta
yarn add react-nesta
🔨 试例
使用方法和 React-Redux 一样,通过 Provider 传递 store,接着使用 connect 方法包裹组件即可将 state 和 put 映射到组件的 props 中。
import React from 'react'
import ReactDOM from 'react-dom'
import { createStore } from 'nesta'
import { Provider, connect } from 'react-nesta'
import PropTypes from 'prop-types'
const store = createStore({
count: 0
})
class App extends React.Component {
static propTypes = {
count: PropTypes.number.isRequired,
}
handlePlus = () => {
this.props.put(state => {
state.count = state.count + 1
})
}
render() {
return (
<div>
{this.props.count}
<button onClick={this.handlePlus}>plus</button>
</div>
)
}
}
const WrappedApp = connect(state => ({ count: state.count }))(App)
ReactDOM.render(
<Provider store={store}>
<WrappedApp />
</Provider>,
document.getElementById('root'))
1.0.0
5 years ago