mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
82 lines
20 KiB
Markdown
82 lines
20 KiB
Markdown
---
|
|
id: sectionlist
|
|
title: SectionList
|
|
category: Components
|
|
permalink: docs/sectionlist.html
|
|
---
|
|
<div><div><p>A performant interface for rendering sectioned lists, supporting the most handy features:</p><ul><li>Fully cross-platform.</li><li>Configurable viewability callbacks.</li><li>List header support.</li><li>List footer support.</li><li>Item separator support.</li><li>Section header support.</li><li>Section separator support.</li><li>Heterogeneous data and item rendering support.</li><li>Pull to Refresh.</li><li>Scroll loading.</li></ul><p>If you don't need section support and want a simpler interface, use
|
|
<a href="/react-native/docs/flatlist.html" target=""><code><FlatList></code></a>.</p><p>Simple Examples:</p><div class="prism language-javascript"><span class="token operator"><</span>SectionList
|
|
renderItem<span class="token operator">=</span><span class="token punctuation">{</span><span class="token punctuation">(</span><span class="token punctuation">{</span>item<span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token operator"><</span>ListItem title<span class="token operator">=</span><span class="token punctuation">{</span>item<span class="token punctuation">.</span>title<span class="token punctuation">}</span> <span class="token operator">/</span><span class="token operator">></span><span class="token punctuation">}</span>
|
|
renderSectionHeader<span class="token operator">=</span><span class="token punctuation">{</span><span class="token punctuation">(</span><span class="token punctuation">{</span>section<span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token operator"><</span>H1 title<span class="token operator">=</span><span class="token punctuation">{</span>section<span class="token punctuation">.</span>title<span class="token punctuation">}</span> <span class="token operator">/</span><span class="token operator">></span><span class="token punctuation">}</span>
|
|
sections<span class="token operator">=</span><span class="token punctuation">{</span><span class="token punctuation">[</span><span class="token comment" spellcheck="true"> // homogenous rendering between sections
|
|
</span> <span class="token punctuation">{</span>data<span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token operator">...</span><span class="token punctuation">]</span><span class="token punctuation">,</span> title<span class="token punctuation">:</span> <span class="token operator">...</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
|
|
<span class="token punctuation">{</span>data<span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token operator">...</span><span class="token punctuation">]</span><span class="token punctuation">,</span> title<span class="token punctuation">:</span> <span class="token operator">...</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
|
|
<span class="token punctuation">{</span>data<span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token operator">...</span><span class="token punctuation">]</span><span class="token punctuation">,</span> title<span class="token punctuation">:</span> <span class="token operator">...</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
|
|
<span class="token punctuation">]</span><span class="token punctuation">}</span>
|
|
<span class="token operator">/</span><span class="token operator">></span>
|
|
|
|
<span class="token operator"><</span>SectionList
|
|
sections<span class="token operator">=</span><span class="token punctuation">{</span><span class="token punctuation">[</span><span class="token comment" spellcheck="true"> // heterogeneous rendering between sections
|
|
</span> <span class="token punctuation">{</span>data<span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token operator">...</span><span class="token punctuation">]</span><span class="token punctuation">,</span> title<span class="token punctuation">:</span> <span class="token operator">...</span><span class="token punctuation">,</span> renderItem<span class="token punctuation">:</span> <span class="token operator">...</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
|
|
<span class="token punctuation">{</span>data<span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token operator">...</span><span class="token punctuation">]</span><span class="token punctuation">,</span> title<span class="token punctuation">:</span> <span class="token operator">...</span><span class="token punctuation">,</span> renderItem<span class="token punctuation">:</span> <span class="token operator">...</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
|
|
<span class="token punctuation">{</span>data<span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token operator">...</span><span class="token punctuation">]</span><span class="token punctuation">,</span> title<span class="token punctuation">:</span> <span class="token operator">...</span><span class="token punctuation">,</span> renderItem<span class="token punctuation">:</span> <span class="token operator">...</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
|
|
<span class="token punctuation">]</span><span class="token punctuation">}</span>
|
|
<span class="token operator">/</span><span class="token operator">></span></div><p>This is a convenience wrapper around <a href="docs/virtualizedlist.html" target="_blank"><code><VirtualizedList></code></a>,
|
|
and thus inherits its props (as well as those of <code>ScrollView</code>) that aren't explicitly listed
|
|
here, along with the following caveats:</p><ul><li>Internal state is not preserved when content scrolls out of the render window. Make sure all
|
|
your data is captured in the item data or external stores like Flux, Redux, or Relay.</li><li>This is a <code>PureComponent</code> which means that it will not re-render if <code>props</code> remain shallow-
|
|
equal. Make sure that everything your <code>renderItem</code> function depends on is passed as a prop
|
|
(e.g. <code>extraData</code>) that is not <code>===</code> after updates, otherwise your UI may not update on
|
|
changes. This includes the <code>data</code> prop and parent component state.</li><li>In order to constrain memory and enable smooth scrolling, content is rendered asynchronously
|
|
offscreen. This means it's possible to scroll faster than the fill rate and momentarily see
|
|
blank content. This is a tradeoff that can be adjusted to suit the needs of each application,
|
|
and we are working on improving it behind the scenes.</li><li>By default, the list looks for a <code>key</code> prop on each item and uses that for the React key.
|
|
Alternatively, you can provide a custom <code>keyExtractor</code> prop.</li></ul></div><h3><a class="anchor" name="props"></a>Props <a class="hash-link" href="docs/sectionlist.html#props">#</a></h3><div class="props"><div class="prop"><h4 class="propTitle"><a class="anchor" name="itemseparatorcomponent"></a>ItemSeparatorComponent?: <span class="propType"><span>?ReactClass<any></span></span> <a class="hash-link" href="docs/sectionlist.html#itemseparatorcomponent">#</a></h4><div><p>Rendered in between each item, but not at the top or bottom. By default, <code>highlighted</code>,
|
|
<code>section</code>, and <code>[leading/trailing][Item/Separator]</code> props are provided. <code>renderItem</code> provides
|
|
<code>separators.highlight</code>/<code>unhighlight</code> which will update the <code>highlighted</code> prop, but you can also
|
|
add custom props with <code>separators.updateProps</code>.</p></div></div><div class="prop"><h4 class="propTitle"><a class="anchor" name="listemptycomponent"></a>ListEmptyComponent?: <span class="propType"><span>?<span><span>ReactClass<any> | </span>React.Element<any></span></span></span> <a class="hash-link" href="docs/sectionlist.html#listemptycomponent">#</a></h4><div><p>Rendered when the list is empty. Can be a React Component Class, a render function, or
|
|
a rendered element.</p></div></div><div class="prop"><h4 class="propTitle"><a class="anchor" name="listfootercomponent"></a>ListFooterComponent?: <span class="propType"><span>?<span><span>ReactClass<any> | </span>React.Element<any></span></span></span> <a class="hash-link" href="docs/sectionlist.html#listfootercomponent">#</a></h4><div><p>Rendered at the very end of the list. Can be a React Component Class, a render function, or
|
|
a rendered element.</p></div></div><div class="prop"><h4 class="propTitle"><a class="anchor" name="listheadercomponent"></a>ListHeaderComponent?: <span class="propType"><span>?<span><span>ReactClass<any> | </span>React.Element<any></span></span></span> <a class="hash-link" href="docs/sectionlist.html#listheadercomponent">#</a></h4><div><p>Rendered at the very beginning of the list. Can be a React Component Class, a render function, or
|
|
a rendered element.</p></div></div><div class="prop"><h4 class="propTitle"><a class="anchor" name="sectionseparatorcomponent"></a>SectionSeparatorComponent?: <span class="propType"><span>?ReactClass<any></span></span> <a class="hash-link" href="docs/sectionlist.html#sectionseparatorcomponent">#</a></h4><div><p>Rendered at the top and bottom of each section (note this is different from
|
|
<code>ItemSeparatorComponent</code> which is only rendered between items). These are intended to separate
|
|
sections from the headers above and below and typically have the same highlight response as
|
|
<code>ItemSeparatorComponent</code>. Also receives <code>highlighted</code>, <code>[leading/trailing][Item/Separator]</code>,
|
|
and any custom props from <code>separators.updateProps</code>.</p></div></div><div class="prop"><h4 class="propTitle"><a class="anchor" name="extradata"></a>extraData?: <span class="propType">any</span> <a class="hash-link" href="docs/sectionlist.html#extradata">#</a></h4><div><p>A marker property for telling the list to re-render (since it implements <code>PureComponent</code>). If
|
|
any of your <code>renderItem</code>, Header, Footer, etc. functions depend on anything outside of the
|
|
<code>data</code> prop, stick it here and treat it immutably.</p></div></div><div class="prop"><h4 class="propTitle"><a class="anchor" name="initialnumtorender"></a>initialNumToRender: <span class="propType">number</span> <a class="hash-link" href="docs/sectionlist.html#initialnumtorender">#</a></h4><div><p>How many items to render in the initial batch. This should be enough to fill the screen but not
|
|
much more. Note these items will never be unmounted as part of the windowed rendering in order
|
|
to improve perceived performance of scroll-to-top actions.</p></div></div><div class="prop"><h4 class="propTitle"><a class="anchor" name="inverted"></a>inverted?: <span class="propType"><span>?boolean</span></span> <a class="hash-link" href="docs/sectionlist.html#inverted">#</a></h4><div><p>Reverses the direction of scroll. Uses scale transforms of -1.</p></div></div><div class="prop"><h4 class="propTitle"><a class="anchor" name="keyextractor"></a>keyExtractor: <span class="propType">(item: Item, index: number) => string</span> <a class="hash-link" href="docs/sectionlist.html#keyextractor">#</a></h4><div><p>Used to extract a unique key for a given item at the specified index. Key is used for caching
|
|
and as the react key to track item re-ordering. The default extractor checks item.key, then
|
|
falls back to using the index, like react does. Note that this sets keys for each item, but
|
|
each overall section still needs its own key.</p></div></div><div class="prop"><h4 class="propTitle"><a class="anchor" name="legacyimplementation"></a>legacyImplementation?: <span class="propType"><span>?boolean</span></span> <a class="hash-link" href="docs/sectionlist.html#legacyimplementation">#</a></h4></div><div class="prop"><h4 class="propTitle"><a class="anchor" name="onendreached"></a>onEndReached?: <span class="propType"><span>?(info: {distanceFromEnd: number}) => void</span></span> <a class="hash-link" href="docs/sectionlist.html#onendreached">#</a></h4><div><p>Called once when the scroll position gets within <code>onEndReachedThreshold</code> of the rendered
|
|
content.</p></div></div><div class="prop"><h4 class="propTitle"><a class="anchor" name="onendreachedthreshold"></a>onEndReachedThreshold?: <span class="propType"><span>?number</span></span> <a class="hash-link" href="docs/sectionlist.html#onendreachedthreshold">#</a></h4><div><p>How far from the end (in units of visible length of the list) the bottom edge of the
|
|
list must be from the end of the content to trigger the <code>onEndReached</code> callback.
|
|
Thus a value of 0.5 will trigger <code>onEndReached</code> when the end of the content is
|
|
within half the visible length of the list.</p></div></div><div class="prop"><h4 class="propTitle"><a class="anchor" name="onrefresh"></a>onRefresh?: <span class="propType"><span>?() => void</span></span> <a class="hash-link" href="docs/sectionlist.html#onrefresh">#</a></h4><div><p>If provided, a standard RefreshControl will be added for "Pull to Refresh" functionality. Make
|
|
sure to also set the <code>refreshing</code> prop correctly.</p></div></div><div class="prop"><h4 class="propTitle"><a class="anchor" name="onviewableitemschanged"></a>onViewableItemsChanged?: <span class="propType"><span>?(info: {
|
|
viewableItems: Array<ViewToken>,
|
|
changed: Array<ViewToken>,
|
|
}) => void</span></span> <a class="hash-link" href="docs/sectionlist.html#onviewableitemschanged">#</a></h4><div><p>Called when the viewability of rows changes, as defined by the
|
|
<code>viewabilityConfig</code> prop.</p></div></div><div class="prop"><h4 class="propTitle"><a class="anchor" name="refreshing"></a>refreshing?: <span class="propType"><span>?boolean</span></span> <a class="hash-link" href="docs/sectionlist.html#refreshing">#</a></h4><div><p>Set this true while waiting for new data from a refresh.</p></div></div><div class="prop"><h4 class="propTitle"><a class="anchor" name="removeclippedsubviews"></a>removeClippedSubviews?: <span class="propType">boolean</span> <a class="hash-link" href="docs/sectionlist.html#removeclippedsubviews">#</a></h4><div><p>Note: may have bugs (missing content) in some circumstances - use at your own risk.</p><p>This may improve scroll performance for large lists.</p></div></div><div class="prop"><h4 class="propTitle"><a class="anchor" name="renderitem"></a>renderItem: <span class="propType">(info: {
|
|
item: Item,
|
|
index: number,
|
|
section: SectionT,
|
|
separators: {
|
|
highlight: () => void,
|
|
unhighlight: () => void,
|
|
updateProps: (select: 'leading' | 'trailing', newProps: Object) => void,
|
|
},
|
|
}) => ?React.Element<any></span> <a class="hash-link" href="docs/sectionlist.html#renderitem">#</a></h4><div><p>Default renderer for every item in every section. Can be over-ridden on a per-section basis.</p></div></div><div class="prop"><h4 class="propTitle"><a class="anchor" name="rendersectionfooter"></a>renderSectionFooter?: <span class="propType"><span>?(info: {section: SectionT}) => ?React.Element<any></span></span> <a class="hash-link" href="docs/sectionlist.html#rendersectionfooter">#</a></h4><div><p>Rendered at the bottom of each section.</p></div></div><div class="prop"><h4 class="propTitle"><a class="anchor" name="rendersectionheader"></a>renderSectionHeader?: <span class="propType"><span>?(info: {section: SectionT}) => ?React.Element<any></span></span> <a class="hash-link" href="docs/sectionlist.html#rendersectionheader">#</a></h4><div><p>Rendered at the top of each section. These stick to the top of the <code>ScrollView</code> by default on
|
|
iOS. See <code>stickySectionHeadersEnabled</code>.</p></div></div><div class="prop"><h4 class="propTitle"><a class="anchor" name="sections"></a>sections: <span class="propType">$ReadOnlyArray<SectionT></span> <a class="hash-link" href="docs/sectionlist.html#sections">#</a></h4><div><p>The actual data to render, akin to the <code>data</code> prop in <a href="/react-native/docs/flatlist.html" target=""><code><FlatList></code></a>.</p><p>General shape:</p><div class="prism language-javascript">sections<span class="token punctuation">:</span> $ReadOnlyArray<span class="token operator"><</span><span class="token punctuation">{</span>
|
|
data<span class="token punctuation">:</span> $ReadOnlyArray<span class="token operator"><</span>SectionItem<span class="token operator">></span><span class="token punctuation">,</span>
|
|
renderItem<span class="token operator">?</span><span class="token punctuation">:</span> <span class="token punctuation">(</span><span class="token punctuation">{</span>item<span class="token punctuation">:</span> SectionItem<span class="token punctuation">,</span> <span class="token operator">...</span><span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token operator">?</span>React<span class="token punctuation">.</span>Element<span class="token operator"><</span><span class="token operator">*</span><span class="token operator">></span><span class="token punctuation">,</span>
|
|
ItemSeparatorComponent<span class="token operator">?</span><span class="token punctuation">:</span> <span class="token operator">?</span>ReactClass<span class="token operator"><</span><span class="token punctuation">{</span>highlighted<span class="token punctuation">:</span> boolean<span class="token punctuation">,</span> <span class="token operator">...</span><span class="token punctuation">}</span><span class="token operator">></span><span class="token punctuation">,</span>
|
|
<span class="token punctuation">}</span><span class="token operator">></span></div></div></div><div class="prop"><h4 class="propTitle"><a class="anchor" name="stickysectionheadersenabled"></a>stickySectionHeadersEnabled?: <span class="propType">boolean</span> <a class="hash-link" href="docs/sectionlist.html#stickysectionheadersenabled">#</a></h4><div><p>Makes section headers stick to the top of the screen until the next one pushes it off. Only
|
|
enabled by default on iOS because that is the platform standard there.</p></div></div></div><span><h3><a class="anchor" name="methods"></a>Methods <a class="hash-link" href="docs/sectionlist.html#methods">#</a></h3><div class="props"><div class="prop"><h4 class="methodTitle"><a class="anchor" name="scrolltolocation"></a>scrollToLocation<span class="methodType">(params: object)</span> <a class="hash-link" href="docs/sectionlist.html#scrolltolocation">#</a></h4><div><p>Scrolls to the item at the specified <code>sectionIndex</code> and <code>itemIndex</code> (within the section)
|
|
positioned in the viewable area such that <code>viewPosition</code> 0 places it at the top (and may be
|
|
covered by a sticky header), 1 at the bottom, and 0.5 centered in the middle. <code>viewOffset</code> is a
|
|
fixed number of pixels to offset the final target position, e.g. to compensate for sticky
|
|
headers.</p><p>Note: cannot scroll to locations outside the render window without specifying the
|
|
<code>getItemLayout</code> prop.</p></div></div><div class="prop"><h4 class="methodTitle"><a class="anchor" name="recordinteraction"></a>recordInteraction<span class="methodType">()</span> <a class="hash-link" href="docs/sectionlist.html#recordinteraction">#</a></h4><div><p>Tells the list an interaction has occured, which should trigger viewability calculations, e.g.
|
|
if <code>waitForInteractions</code> is true and the user has not scrolled. This is typically called by
|
|
taps on items or by navigation actions.</p></div></div><div class="prop"><h4 class="methodTitle"><a class="anchor" name="flashscrollindicators"></a>flashScrollIndicators<span class="methodType">()</span> <a class="hash-link" href="docs/sectionlist.html#flashscrollindicators">#</a></h4><div><p>Displays the scroll indicators momentarily.</p></div></div></div></span></div> |