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
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
60
Other Decks in Programming
See All in Programming
IBM Bob Dojo #1 仕様駆動開発入門
oniak3ibm
PRO
0
380
[Rails World 2026] Durable orchestration on Rails: from continuation to workflow
palkan
1
730
【加筆修正版】Laravel のアプリケーションをどこにデプロイするか #phpcon_ehime
akase244
0
150
速く作れる。その次は、速く確かめられる開発へ 〜AIネイティブ開発を支える、Shift Down〜 / Can build fast. Next, moving to development where we can verify fast.
rkaga
8
6k
TechTown - Creating Composable Callables in Contemporary C++
rollbear
0
110
カツオ、ご期待ください
suneo3476
0
180
機械に任せるテスト、人が見るテスト ⽣成AIで引き直した、運⽤保守フェーズの線引き
dske104
0
180
設計次第でAIコードの読む量は減らせる / designing-for-code-reading
minodriven
35
16k
Agents on Rails - Rails at Scale 2026
irinanazarova
0
540
Kotlinの15年とServer-Side Kotlin
n_takehata
1
140
Atomic Design, Enforced: Scaling Mobile Design Systems (next.app devCon / droidCon Berlin 2026)
steliosf
PRO
0
170
AI時代のコードレビューは人に向けるな、仕組みに向けろ
texmeijin
5
3.6k
Featured
See All Featured
B2B Lead Gen: Tactics, Traps & Triumph
marketingsoph
1
280
The World Runs on Bad Software
bkeepers
PRO
72
12k
Tell your own story through comics
letsgokoyo
1
1.1k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
133
20k
Marketing Yourself as an Engineer | Alaka | Gurzu
gurzu
0
330
Building an army of robots
kneath
307
47k
コードの90%をAIが書く世界で何が待っているのか / What awaits us in a world where 90% of the code is written by AI
rkaga
63
46k
Marketing to machines
jonoalderson
1
5.9k
How to build a perfect <img>
jonoalderson
1
6.1k
How to Think Like a Performance Engineer
csswizardry
28
2.9k
WENDY [Excerpt]
tessaabrams
14
40k
Mobile First: as difficult as doing things right
swwweet
225
10k
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