我通過導航傳遞并宣告了一個變數(專案),但在“回傳”內部,它說未定義。
這里變數(專案)
{id: "001"
name: "name"}
代碼
import React, {useState,useEffect} from "react";
import {Text, View} from "react-native";
import styled from "styled-components/native";
import { TouchableWithoutFeedback, TouchableOpacity} from "react-native-gesture-handler";
const PlaySystem= ({navigation,route}) => {
const {data}= useState(null)
useEffect(()=>{
let {data} =route.params;
console.log("first",data.id)
},[])
return (
<Container>
<StatusBar barStyle="Light-content"/>
<View>
<Text>{data?.name}</Text>
{ console.log("second",data.id) }
</View>
</Container>);}
export default PlaySystem;
console.log 顯示:
second undefined
first 001
uj5u.com熱心網友回復:
像這樣做 -
const PlaySystem= ({navigation,route}) => {
let {data} =route.params;
return (
<Container>
<StatusBar barStyle="Light-content"/>
<View>
<Text>{data?.name}</Text>
</View>
</Container>);}
export default PlaySystem;
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/427165.html
標籤:javascript 反应式
上一篇:如何使用默認值擴展給定陣列?
下一篇:使用字典映射物件陣列
