Quantcast
Channel: Active questions tagged react-native+typescript - Stack Overflow
Viewing all articles
Browse latest Browse all 6287

How to pass in constants from other files in React Native Typescript

$
0
0

I want to pass in the countryCases and countryDeaths constants that use useState from the Data.tsx file. I want to pass them in and use them in the OtherScreen.tsx file. How can I do this?

Data.tsx

interface DataCardProps {  onPress: () => void;}const Covid19DataCard = ({ onPress }: DataCardProps) => {  const [earliest2, setEarliest2] = useState([]);  const [countryDeaths, setcountryDeaths] = useState(Number);  const [countryCases, setcountryCases] = useState(Number);  useEffect(() => {    axios      .get("https://coronavirus-19-api.herokuapp.com/countries")      .then((response) => {        setEarliest2(response.data);        const enteredCountryName = countryNameshort;        const countryArray = response.data.filter(          (item) => item.country === enteredCountryName        );        const resCountryDeaths = countryArray[0].deaths;        setcountryDeaths(resCountryDeaths);        const resCountryCases = countryArray[0].cases;        setcountryCases(resCountryCases);      })      .catch((err) => {        console.log(err);      });  }, []);}

OtherScreen.tsx

import Data from './Data';const OtherScreen = ({ navigation,}: StackNavigationProps<Routes, "SearchScreen">) => {<Text>{Data.countryCases}</Text> //???}export default OtherScreen;

They both use different props, and if I try to pass in Data.countryCases, I get an error Property 'countryCases' does not exist on type '({ onPress }: DataCardProps) => Element'.Any help is appreciated :)


Viewing all articles
Browse latest Browse all 6287

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>