Skip to content

Instantly share code, notes, and snippets.

@mariusandra
Created August 9, 2017 14:48
Show Gist options
  • Save mariusandra/1947f828fefc511003befe69a85d4e97 to your computer and use it in GitHub Desktop.
Save mariusandra/1947f828fefc511003befe69a85d4e97 to your computer and use it in GitHub Desktop.
// features-logic.js
import PropTypes from 'prop-types'
import { kea } from 'kea'
export default kea({
actions: () => ({
toggleFeature: (feature) => ({ feature })
}),
reducers: ({ actions }) => ({
features: [{}, PropTypes.object, {
[actions.toggleFeature]: (state, payload) => {
const { feature } = payload
return {
...state,
[feature]: !state[feature]
}
}
}]
})
})
// index.js
import React, { Component } from 'react'
import { connect } from 'kea'
import featuresLogic from '../features-logic'
@connect({
actions: [
featuresLogic, [
'toggleFeature'
]
],
props: [
featuresLogic, [
'features'
]
]
})
export default class ConnectedToggle extends Component {
render () {
const { features } = this.props
const { toggleFeature } = this.actions
return (
<div>
<p>{features.something ? 'Something enabled' : 'Something disabled'}</p>
<button onClick={() => toggleFeature('something')}>Toggle something</button>
</div>
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment