我认为我的代码是正确的,但事实证明,它给了我未定义,然而,当我点击一个按钮去'列表';它的工作原理!只是我不能从touchableacityonpress中做。这是我的代码
import { StyleSheet, Text, View,TouchableOpacity } from 'react-native'
import {useSelector, useDispatch} from 'react-redux'
import {fetchDogsAsync} from '../store/action/index'
const DogsList = ({navigation}) => {
const dispatch = useDispatch()
const {dogs,isLoading} = useSelector(state => state)
useEffect(() => {
dispatch(fetchDogsAsync())
}, [dispatch])
console.log(dogs,isLoading);
return (
<View>
{
isLoading ? <Text>Loading...</Text> : (dogs?.message).map((dog,idx)=> {
return (
<TouchableOpacity key={dog} onPress={() => navigation.reset({
index: 0,
routes: [{name: 'List', params: {breed: dog}}],
})}><Text>{dog}</Text></TouchableOpacity>
)
})
}
</View>
)
}
export default DogsList
const styles = StyleSheet.create({})
<Navigator.Screen
name="initial"
component={ListScreen}
options={{ title: "", headerShown: false }}
/>
只有在Screen标签中使用的组件才会收到导航道具。这里ListScreen将接收导航道具。然而,ListScreen的子组件不会接收导航道具。但你可以在组件树的任何地方使用useNavigation钩子,它会给你导航对象。你可以在他们的官方文档中了解更多。
https://reactnavigation.org/docs/use-navigation/