Last active
October 20, 2018 01:30
-
-
Save itaditya/79644c2d3d72e019b8d29689363ceb5c to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import React, { Component, createContext } from 'react'; | |
const context = createContext({ | |
activeTabId: '', | |
changeTab: () => {} | |
}); | |
const { Provider, Consumer } = context; | |
const Tab = ({ id, children }) => ( | |
<Consumer> | |
{({ changeTab }) => <div onClick={() => changeTab(id)}>{children}</div>} | |
</Consumer> | |
); | |
const TabPanel = ({ whenActive, children }) => ( | |
<Consumer> | |
{({ activeTabId }) => (activeTabId === whenActive ? children : null)} | |
</Consumer> | |
); | |
class TabSwitcher extends Component { | |
state = { | |
activeTabId: 'a' | |
}; | |
changeTab = newTabId => { | |
this.setState({ | |
activeTabId: newTabId | |
}); | |
}; | |
render() { | |
return ( | |
<Provider | |
value={{ | |
activeTabId: this.state.activeTabId, | |
changeTab: this.changeTab | |
}} | |
> | |
{this.props.children} | |
</Provider> | |
); | |
} | |
} | |
export default TabSwitcher; | |
export { Tab, TabPanel }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment