Kristofer Giltvedt Selbekk
I'm a full stack engineer that just happens to love React, CSS and front end in general
@selbekk
4 patterns
Quick introduction
Tasks!
Quick summary
Implicit props via React.cloneElement
Higher order components
github.com/selbekk/react-workshop
React.cloneElement(
element,
{ extra: 'props', to: 'apply' },
children,
);React.Children.map(
props.children,
child => cloneElement(
child,
{ extra: 'props', to: 'apply' },
),
);github.com/selbekk/react-workshop
const withHOC = (TargetComponent) => {
return (props) => (
<TargetComponent {...props} />
);
};const withHOC = (TargetComponent) => {
class WrapperComponent extends Component {
render() {
return (
<TargetComponent {...this.props} />
);
}
};
return WrapperComponent;
};github.com/selbekk/react-workshop
<Cheers
render={(sayCheers) => (
<button onClick={sayCheers}>
šŗ Cheers!
</button>
)}
/>
const Cheers = props => {
const sayCheers = alert('Cheers!');
return props.render(sayCheers);
};
<Cheers
render={(sayCheers) => (
<button onClick={sayCheers}>
šŗ Cheers!
</button>
)}
/>
<Cheers
children={(sayCheers) => (
<button onClick={sayCheers}>
šŗ Cheers!
</button>
)}
/>
<Cheers>
{(sayCheers) => (
<button onClick={sayCheers}>
šŗ Cheers!
</button>
)}
</Cheers>
github.com/selbekk/react-workshop
const MyContext = React.createContext('default');
const MyContext = React.createContext(42);
const MyContext = React.createContext({
myValue: 'some value',
updateValue: f => f,
});
const {
Provider,
Consumer,
} = React.createContext('default');
const SmileyContext = React.createContext('š');
class SmileyProvider extends Component {
state =Ā { smiley: 'š' };
render() {
return (
<SmileyContext.Provider
value={this.state.smiley}
>
{props.children}
</SmileyContext.Provider>
);
}
}
const SmileyContext = React.createContext('š');
const SmileyProvider = ...;
const App = () => (
<SmileyProvider>
<LotsOfOtherComponents>
<SmileyContext.Consumer>
(smiley => (
<span>My smiley is {smiley}</span>
)}
</SmileyContext.Consumer>
</LotsOfOtherComponents>
</SmileyProvider>
);
const SmileyContext = React.createContext('š');
const SmileyProvider = ...;
const withSmiley = (TargetComponent) => {
return (props) => (
<SmileyContext.Consumer>
{smiley => (
<TargetComponent
{...props}
smiley={smiley}
/>
)}
</SmileyContext.Consumer>
);
};
const App = () => (
<SmileyProvider>
<LotsOfOtherComponents>
<SmileyContext.Consumer>
(smiley => (
<span>My smiley is {smiley}</span>
)}
</SmileyContext.Consumer>
</LotsOfOtherComponents>
</SmileyProvider>
);
github.com/selbekk/react-workshop
By Kristofer Giltvedt Selbekk
I'm a full stack engineer that just happens to love React, CSS and front end in general