Complete the code to import the React hook needed for pull-to-refresh.
import React, { [1] } from 'react';
The useCallback hook is used to memoize the refresh function for pull-to-refresh.
Complete the code to set the refreshing state variable.
const [refreshing, [1]] = React.useState(false);The setter function for the refreshing state is conventionally named setRefreshing.
Fix the error in the refresh function to correctly update the refreshing state.
const onRefresh = React.useCallback(() => {
[1](true);
setTimeout(() => [1](false), 2000);
}, []);The function setRefreshing must be called to update the refreshing state.
Fill both blanks to add pull-to-refresh to the FlatList component.
<FlatList
data={data}
renderItem={renderItem}
keyExtractor={item => item.id}
refreshing=[1]
onRefresh=[2]
/>The refreshing prop takes the boolean state, and onRefresh takes the function to call on pull.
Fill all three blanks to complete the pull-to-refresh example with FlatList.
import React, { [1] } from 'react'; import { FlatList } from 'react-native'; export default function App() { const [refreshing, [2]] = React.useState(false); const onRefresh = React.useCallback(() => { [2](true); setTimeout(() => [2](false), 1500); }, []); return ( <FlatList data={[{id: '1'}, {id: '2'}]} keyExtractor={item => item.id} renderItem={({item}) => <></>} refreshing={refreshing} onRefresh={onRefresh} /> ); }
We import useCallback, use useState to create refreshing and setRefreshing, and call setRefreshing to update state.