Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
React component patterns
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
Dawid Jankowiak
October 17, 2017
Programming
130
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
React component patterns
Dawid Jankowiak
October 17, 2017
More Decks by Dawid Jankowiak
See All by Dawid Jankowiak
React Component Patterns WDI version
jankowiakdawid
1
59
Other Decks in Programming
See All in Programming
AIエージェント時代のコードレビューを設計する
nogu66
6
2.7k
速く作れる。その次は、速く確かめられる開発へ 〜AIネイティブ開発を支える、Shift Down〜 / Can build fast. Next, moving to development where we can verify fast.
rkaga
5
3.2k
SONY CISC-NEWS NWS-1750 + NWB-225 フレームバッファの NetBSD/news68k ドライバ実装 / OSC2026Hiroshima
tsutsui
0
110
Augmenting AI with the Power of Jakarta EE
ivargrimstad
0
120
大喜利で理解するLLM as a Judge / Understanding LLM-as-a-Judge through Ogiri
rockname
0
120
動作中のプログラムの中身をリアルタイムに覗く / Realtime Debugger for CSharp with Roslyn
prota
0
270
仕様駆動開発による爆速プロダクト開発 / Bakusoku Spec Driven Development
kobakei
0
120
iOSDC2026登壇資料.pdf
riofujimon
0
170
【高い買い物LT会】初任給で話題の国産フィジカルAIを買った話
akagami
PRO
0
160
巨大モノリシックアプリ モダン化大作戦
ktcryomm
1
1k
AIは賢い。でも実行環境は? CLIおじさんがAI時代に伝えたいこと ~ CLIおじさんがAI時代に伝えたいこと ~
curekoshimizu
1
240
技術的負債を組織課題として解く-増えすぎたマイクロサービスとの戦い-
reimaru
1
1.9k
Featured
See All Featured
Building Adaptive Systems
keathley
44
3.2k
Mozcon NYC 2025: Stop Losing SEO Traffic
samtorres
1
550
Why Our Code Smells
bkeepers
PRO
340
58k
Color Theory Basics | Prateek | Gurzu
gurzu
1
470
What’s in a name? Adding method to the madness
productmarketing
PRO
24
4.2k
Information Architects: The Missing Link in Design Systems
soysaucechin
1
1.1k
The MySQL Ecosystem @ GitHub 2015
samlambert
251
13k
Statistics for Hackers
jakevdp
799
230k
The Curse of the Amulet
leimatthew05
3
14k
Building Flexible Design Systems
yeseniaperezcruz
330
41k
Redefining SEO in the New Era of Traffic Generation
szymonslowik
1
420
Leadership Guide Workshop - DevTernity 2021
reverentgeek
1
370
Transcript
@jankowiak_dawid
Higher Order Component Function as a Child (Render Function) Compound
Components Reducer Component
Connect(react-redux) Recompose Redux-Form Function that takes a component as a
parameter and returns another component
const ApplicantInfoPage = ({ data, isLoading, error }) => {...};
export default withFetch( ownProps => `/api/applicants/${ownProps.match.props.id}` )(ApplicantInfoPage);
const withFetch = urlGetter => BaseComponent => class WithFetch extends
React.Component { state = { isLoading: true, error: null, data: null }; static displayName = `WithFetch(${wrapDisplayName( BaseComponent )})`;
componentDidMount() { axios .get(urlGetter(this.props)) .then(({ data }) => this.setState({ data,
isLoading: false })) .catch(error => this.setState({ error, isLoading: false })); }
render() { if (this.state.isLoading === true) { return <Loading />;
} if (this.state.error !== null) { return <ErrorPanel />; } return <BaseComponent {...this.props} data={this.state.data} />; } };
Lets you mix behaviours Simple to use Composable (like regular
function composition) Applicable before use (cannot be used in render function) Refs aren’t passed through Static methods must be copied over
https://github.com/acdlite/recompose/blob/master/docs/performance.md https://medium.com/@dan_abramov/mixins-are-dead-long-live-higher-order-components-94a0d2f9e750 More on the topic:
Downshift React-Motion React-Router v4 react-virtualized Passing a function to a
component (as a prop or children of component) that tells it what to render
<Paging items={itemsArray} itemsPerPage={10} render={item => ( <div>{item.name}</div> )} />; <Paging
items={itemsArray} itemsPerPage={10} > {item => ( <div>{item.name}</div> )} </Paging>;
class Paging extends Component { … render() { const items
= this.getItems(); return ( <div> {items.map(this.prop.render)} {this.renderButtons()} </div> ); } } class Paging extends Component { … render() { const items = this.getItems(); return ( <div> {items.map(this.prop.children)} {this.renderButtons()} </div> ); } }
Used as regular components Simple to implement Dynamic in nature
Nesting (hell?) Returning large component tree may introduce performance problems
https://cdb.reacttraining.com/use-a-render-prop-50de598f11ce https://blog.kentcdodds.com/introducing-downshift-for-react-b1de3fca0817 http://americanexpress.io/faccs-are-an-antipattern/ More on the topic:
Table List Accordion Tabs Carousel Components that share the local
state
<Carousel> <Item> <img src="some-nsfw.png" /> </Item> <Item active> <h1>Bold title</h1>
</Item> </Carousel>;
const Item = ({ children, isHighlighted }) => ( <section
className={isHighlighted ? 'highlighted' : ''}> {children} </section> )
class Carousel extends Component { state = { activeIndex: null
}; render() { const { state: { activeIndex }, props: { children } } = this; return ( <div> <PrevButton onClick={this.handlePreviewClick()} /> {this.getActiveItem(children, activeIndex)} {this.getDottedMenu(children)} <NextButton onClick={this.handleNextClick()} /> </div> ); }
... getActiveItem(items, activeIndex) { const item = React.Children.toArray(items).find((item, index) =>
{ if (activeIndex !== null) return index === activeIndex; return item.props.active === true; }); return React.cloneElement(item, { isHighlighted: true }); } ...
... getDottedMenu(items) { return React.Children.map(items, (item, index) => <DotMenuItem onClick={()
=> this.setState({ activeIndex: index })} /> ); } }
Enables creating general purpose sharable components Require a lot of
imperative manual work Local state harder to manage from outside
React.Children.toArray React.Children.map React.cloneElement React.Children.count React.Children.only React.Children.forEach React.isValidElement
http://mxstbr.blog/2017/02/react-children-deepdive/ https://youtu.be/hEGg-3pIHlE http://developingthoughts.co.uk/using-the-react-children-api/ More on the topic:
Component with State Machine, inspired by ReasonReact, feels like Redux
in component Rediscovered from recompose (bet all familiar with redux had this idea)
class Counter extends ReducerComponent { state = { count: 0
}; increment(_e) { return "increment"; } decrement(_e) { return "decrement"; }
reducer(state, action) { switch (action) { case "increment": return {
count: state.count + 1 }; case "decrement": return { count: state.count - 1 }; } }
render() { return ( <div> Counter: {this.state.count} <button onClick={this.dispatch(this.increment)}>+</button> <button
onClick={this.dispatch(this.decrement)}>-</button> </div> ); } }
class ReducerComponent extends React.Component { reducer(state, action) { return state;
} dispatch(actionCreator) { return (...eventArgs) => { this.setState((prevState, _props) => { this.reducer(prevState, actionCreator(...eventArgs)); }); }; } }
Simple to manage a complex state For small apps/complex local
states Requires familiarity with a reducing state Not needed when other State libraries are present (Redux/MobX)
https://twitter.com/_developit/status/905250269306474497 https://twitter.com/jaredpalmer/status/905170062679662594 https://twitter.com/_developit/status/905427773891727360 https://reasonml.github.io/reason-react/docs/en/state-actions-reducer.html More on the topic:
Higher Order Component Lets you add behaviour to components Function
As a Child (Render Function) Lets you focus on what will be rendered Compound Components Lets you create groups of components with shared state Reducer Component Lets you manage a component state in a single place
@jankowiak_dawid