React native 组件获取未定义的道具
我正在尝试使用导航来导航并将道具传递给另一个组件,但由于某些原因,道具在目标屏幕中变得未定义 我有一个带有平面列表的主屏幕,平面列表上的每个项目将用户移动到第二个屏幕,其中包含列表项目中的相应数据 这是主屏幕平面列表和导航功能:React native 组件获取未定义的道具,react-native,React Native,我正在尝试使用导航来导航并将道具传递给另一个组件,但由于某些原因,道具在目标屏幕中变得未定义 我有一个带有平面列表的主屏幕,平面列表上的每个项目将用户移动到第二个屏幕,其中包含列表项目中的相应数据 这是主屏幕平面列表和导航功能: async function showConversation(company: Company) { const currentUser: User = await StorageHelper.retrieveEntity() as User; const
async function showConversation(company: Company) {
const currentUser: User = await StorageHelper.retrieveEntity() as User;
const from = currentUser.uid;
const to = company.uid;
console.log(from); <-- logs correctly
console.log(to); <-- logs correctly
navigation.navigate('Conversation', {to: to, from: from});
}
return (
<FlatList<Company>
data={companies}
renderItem={({item}) => (
<TouchableOpacity onPress={() => showConversation(item)}>
<CompanyCard company={item} />
</TouchableOpacity>
)}
keyExtractor={(item) => item.uid}
showsVerticalScrollIndicator={false}
/>
)
异步函数showConversation(公司:公司){
const currentUser:User=wait StorageHelper.retrieveEntity()作为用户;
const from=currentUser.uid;
const to=company.uid;
console.log(来自);item.uid}
showsVerticalScrollIndicator={false}
/>
)
这是我的对话屏幕:
interface conversationScreenProps {
navigation: any;
to: string
from: string;
}
const ConversationScreen = ({
navigation,
to,
from,
}: conversationScreenProps) => {
const [messages, setMessages] = useState<Array<IMessage>>([]);
const [chatMessage, setChatMessage] = useState<string>('');
const socket = io('http://127.0.0.1:3000');
console.log(to); <-- logs undefined
console.log(from); <-- logs undefined
}
界面对话屏幕道具{
导航:任何;
致:字符串
来源:字符串;
}
const ConversationScreen=({
航行
到
从…起
}:对话屏幕道具)=>{
const[messages,setMessages]=useState([]);
const[chatMessage,setChatMessage]=useState(“”);
常量套接字=io('http://127.0.0.1:3000');
console.log(to);您应该这样访问它们。因为您不是直接传递道具,而是通过react导航传递道具
let to = navigation.state.params.to
let from = navigation.state.params.from
你应该这样访问道具,因为你不是直接通过道具,而是通过导航
let to = navigation.state.params.to
let from = navigation.state.params.from
参数在路由道具内部传递,而不是作为单独道具传递。
您必须按如下方式访问它
const ConversationScreen = ({
navigation,
route
}: conversationScreenProps) => {
const [messages, setMessages] = useState<Array<IMessage>>([]);
const [chatMessage, setChatMessage] = useState<string>('');
const socket = io('http://127.0.0.1:3000');
console.log(route.params.to); <-- logs undefined
console.log(route.params.from); <-- logs undefined
}
const ConversationScreen=({
航行
路线
}:对话屏幕道具)=>{
const[messages,setMessages]=useState参数在路由道具内部传递,而不是作为单独道具传递。
您必须按如下方式访问它
const ConversationScreen = ({
navigation,
route
}: conversationScreenProps) => {
const [messages, setMessages] = useState<Array<IMessage>>([]);
const [chatMessage, setChatMessage] = useState<string>('');
const socket = io('http://127.0.0.1:3000');
console.log(route.params.to); <-- logs undefined
console.log(route.params.from); <-- logs undefined
}
const ConversationScreen=({
航行
路线
}:对话屏幕道具)=>{
常量[消息,设置消息]=useState谢谢,我现在尝试了这个,但是我得到了一个类型错误,说navigation.state.params
未定义。我现在正在尝试寻找解决方案。谢谢,我现在尝试了这个,但是我得到了一个类型错误,说navigation.state.params
未定义。我正在尝试寻找这个问题的解决方案。在react navigation中ion 5x参数是路由属性非导航的成员。在react导航中,5x参数是路由属性非导航的成员。