Created
August 9, 2017 14:48
-
-
Save mariusandra/1947f828fefc511003befe69a85d4e97 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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