Skip to content

Instantly share code, notes, and snippets.

@itaditya
Last active October 20, 2018 01:30
Show Gist options
  • Save itaditya/79644c2d3d72e019b8d29689363ceb5c to your computer and use it in GitHub Desktop.
Save itaditya/79644c2d3d72e019b8d29689363ceb5c to your computer and use it in GitHub Desktop.
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