We recently implemented Expo Router 文件路由方案, and accumulated quite a bit of experience. Here's a summary for reference, hoping it helps those doing similar work.
Core Concepts
Building on this foundation, we can further optimize:
import React, { useState, useCallback } from 'react'
import { View, Text, FlatList, TouchableOpacity, StyleSheet } from 'react-native'
const ItemList = ({ data, onRefresh }) => {
const [refreshing, setRefreshing] = useState(false)
const handleRefresh = useCallback(async () => {
setRefreshing(true)
await onRefresh()
setRefreshing(false)
}, [onRefresh])
const renderItem = useCallback(({ item }) => (
<TouchableOpacity style={styles.item}>
<Text style={styles.title}>{item.title}</Text>
</TouchableOpacity>
), [])
return (
<FlatList data={data} renderItem={renderItem}
keyExtractor={item => item.id}
refreshing={refreshing} onRefresh={handleRefresh} />
)
}
This pattern is very practical in large projects and can significantly reduce maintenance costs.
In-Depth Analysis
Usage in real projects tends to be more complex:
import React, { useState, useCallback } from 'react'
import { View, Text, FlatList, TouchableOpacity, StyleSheet } from 'react-native'
const ItemList = ({ data, onRefresh }) => {
const [refreshing, setRefreshing] = useState(false)
const handleRefresh = useCallback(async () => {
setRefreshing(true)
await onRefresh()
setRefreshing(false)
}, [onRefresh])
const renderItem = useCallback(({ item }) => (
<TouchableOpacity style={styles.item}>
<Text style={styles.title}>{item.title}</Text>
</TouchableOpacity>
), [])
return (
<FlatList data={data} renderItem={renderItem}
keyExtractor={item => item.id}
refreshing={refreshing} onRefresh={handleRefresh} />
)
}
Through this approach, both the testability and scalability of the code are improved.
Implementation Experience
Here is a complete example:
import React, { useState, useCallback } from 'react'
import { View, Text, FlatList, TouchableOpacity, StyleSheet } from 'react-native'
const ItemList = ({ data, onRefresh }) => {
const [refreshing, setRefreshing] = useState(false)
const handleRefresh = useCallback(async () => {
setRefreshing(true)
await onRefresh()
setRefreshing(false)
}, [onRefresh])
const renderItem = useCallback(({ item }) => (
<TouchableOpacity style={styles.item}>
<Text style={styles.title}>{item.title}</Text>
</TouchableOpacity>
), [])
return (
<FlatList data={data} renderItem={renderItem}
keyExtractor={item => item.id}
refreshing={refreshing} onRefresh={handleRefresh} />
)
}
Pay attention to boundary condition handling, which is critical in production.
Optimization Strategies
The key lies in understanding the core logic:
import React, { useState, useCallback } from 'react'
import { View, Text, FlatList, TouchableOpacity, StyleSheet } from 'react-native'
const ItemList = ({ data, onRefresh }) => {
const [refreshing, setRefreshing] = useState(false)
const handleRefresh = useCallback(async () => {
setRefreshing(true)
await onRefresh()
setRefreshing(false)
}, [onRefresh])
const renderItem = useCallback(({ item }) => (
<TouchableOpacity style={styles.item}>
<Text style={styles.title}>{item.title}</Text>
</TouchableOpacity>
), [])
return (
<FlatList data={data} renderItem={renderItem}
keyExtractor={item => item.id}
refreshing={refreshing} onRefresh={handleRefresh} />
)
}
Performance optimization should be tailored to specific scenarios; not all cases require over-optimization.
Important Notes
We can improve it in the following ways:
import React, { useState, useCallback } from 'react'
import { View, Text, FlatList, TouchableOpacity, StyleSheet } from 'react-native'
const ItemList = ({ data, onRefresh }) => {
const [refreshing, setRefreshing] = useState(false)
const handleRefresh = useCallback(async () => {
setRefreshing(true)
await onRefresh()
setRefreshing(false)
}, [onRefresh])
const renderItem = useCallback(({ item }) => (
<TouchableOpacity style={styles.item}>
<Text style={styles.title}>{item.title}</Text>
</TouchableOpacity>
), [])
return (
<FlatList data={data} renderItem={renderItem}
keyExtractor={item => item.id}
refreshing={refreshing} onRefresh={handleRefresh} />
)
}
This approach has been running stably in production for over six months and has been practically validated.
Summary
- Always verify compatibility before using in production
- In team collaboration, conventions and documentation are more important than the technology itself
- Stay updated with the community; technical solutions need continuous iteration
- Don't adopt new technology just for the sake of it