javascript - 使用上下文返回未定义 - 反应原生
问题描述
我想要一个页面,其中包含从后端获取的数据列表。列表中的每个值都有一个“获取更多详细信息”按钮,单击该按钮应显示每个项目的单个数据。我正在尝试使用 useContext 挂钩将项目数据传递到下一页,但数据在下一页中显示为未定义。
GetOrders.js
export const orderContext = React.createContext()
const GetOrders = ({ navigation }) => {
renderOrdersData = ({ item }) => {
return (
<orderContext.Provider value={ item }>
<TouchableOpacity onPress={() => showAddress(item)} style={[styles.item]}>
<View style={styles.textstyles}>
<View style={styles.contentstyle}>
<Text style={styles.title}>Name: {item.customer.name}</Text>
<Text style={styles.title}>Quantity: {item.orders.quantity} </Text>
<Text style={styles.title}>Contact: {item.customer.contact}</Text>
</View>
<View style={styles.paidbuttonstyles} >
<Button title="Get more details"
onPress={() => navigation.navigate('CustomerOrder') }
/>
</View>
</View>
</TouchableOpacity>
</orderContext.Provider>
);
};
return (
<SafeAreaView style={styles.container}>
<Text style={styles.textcss}> Your Pending orders are </Text>
<FlatList
style={styles.listcolor}
data={orders}
renderItem={renderOrdersData}
/>
</SafeAreaView>
);
}
客户订单.js
import { orderContext } from './GetOrders'
const CustomerOrder = ({ navigation }) => {
let items = useContext(orderContext)
showAddress = () => {
console.log(" inside showadress" + items);
console.log(" inside showadress json " + JSON.stringify(items));
}
return (
<View style={styles.dismissbuttonstyles} >
<Button title="Get Customer address"
onPress={() => showAddress()}
/>
</View>
);
}
每当我按下“GetCustomer address”按钮时,都会出现异常
[Tue Sep 01 2020 19:20:50.443] LOG inside showadressundefined
[Tue Sep 01 2020 19:20:50.459] LOG inside showadress json undefined
[Tue Sep 01 2020 19:20:50.461] ERROR TypeError: undefined is not an object
任何帮助,将不胜感激?谢谢
解决方案
推荐阅读
- c# - 允许在可浏览属性的组合框中复制/粘贴
- css - R shinydashboardPlus 2.0.0 相当于 sidebar_fullCollapse(或 css)
- iis - ASP.net MVC 站点无法在 Windows Server 2019 上运行
- javascript - 如何使用 React hook 修复渲染的钩子比上一次渲染时更多的钩子
- firebase - Firebase 云消息传递对于在 Web 应用程序中开发聊天系统是否可靠?
- performance - Julia - 加快字符串到日期或日期时间的转换
- html - 图像位置推出元素
- html - 内联文本居中对齐
- algorithm - 求解斐波那契数列的非齐次递归关系
- php - php isset 检查中的同一页面发布请求不起作用?