Last active
September 3, 2019 09:02
-
-
Save akoskm/71c234a90dc39823eacd3bb6426164e4 to your computer and use it in GitHub Desktop.
Use React Context to DRY up your components
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
<TableContext.Provider value={product}> | |
<Table | |
products={products} | |
onProductChange={handleProductChange} | |
/> | |
<Sidebar /> | |
</TableContext.Provider> |
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
<Table | |
selectedProduct={product} | |
products={products} | |
onProductChange={handleProductChange} | |
/> | |
<Sidebar product={product} /> |
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
const value = React.useContext(TableContext); |
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
const TableContext = React.createContext(); | |
TableContext.Provider.propTypes = { | |
value: PropTypes.shape({ | |
id: PropTypes.string, | |
name: PropTypes.string, | |
sku: PropTypes.string, | |
}), | |
} | |
export default TableContext; |
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
const TableContext = React.createContext(); | |
export default TableContext; |
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
const Table = ({ selectedProduct, products, onProductChange }) => ( | |
<tbody> | |
{products.map(currProduct => ( | |
<TableRow | |
selected={isSelected(currProduct, selectedProduct)} | |
key={currProduct.id} | |
product={currProduct} | |
onProductChange={onProductChange} | |
/> | |
))} | |
</tbody> | |
); | |
export default Table; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment