Created
September 5, 2019 17:19
-
-
Save v1vendi/37433ad1e73d861b67cd4fbfb53a5b0d to your computer and use it in GitHub Desktop.
Idea of React components composition
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
| const capitalize = s => s[0].toUpperCase() + s.slice(1) | |
| const types = [ | |
| 'primary', | |
| 'secondary', | |
| 'success', | |
| 'danger', | |
| ] | |
| const BasicButton = ({ onClick, children, className }) => ( | |
| <button | |
| class={`btn ${className}`.trim()} | |
| onClick={onClick} | |
| > | |
| {children} | |
| </button> | |
| ) | |
| const Button = {} | |
| types.forEach(type => { | |
| Button[capitalize(type)] = props => <Button type={type} {...props} /> | |
| }) | |
| // usage: | |
| <Button.Primary onClick={() => {}>click me!</Button.Primary> | |
| <Button.Danger onClick={() => {}>click me!</Button.Danger> |
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 from "react"; | |
| import ReactDOM from "react-dom"; | |
| const Title = ({text, onClick}) => <h1 onClick={onClick}>{text}</h1> | |
| const state = { | |
| whatever: { | |
| string: 'title text' | |
| } | |
| } | |
| function connect(stp, dtp) { | |
| return function(Class) { | |
| return () => <Class {...stp(state)} {...dtp} /> | |
| } | |
| } | |
| const someReduxActionCreator = () => {} | |
| const stateToProps = state => ({ text: state.whatever.string }) | |
| const dispatchToProps = { onClick: someReduxActionCreator } | |
| const TitleContainer = connect(stateToProps, dispatchToProps)(Title) | |
| Title.Connected = TitleContainer | |
| // usage: | |
| const component = <Title.Connected></Title.Connected> | |
| ReactDOM.render(component, window.root) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment