These instructions are a condensed version of a screencast on how to build JSX components from JSON.
Unlike the screencast, this script also shows how to simulate this in an Expo Snack. Jump to instructions for snack.
You should only need to make changes to App.js, not Card.js. The technique is slightly different for a Snack, as indicated in section 2 at bottom.
import React, { useState, useEffect } from "react";
import Card from "./components/Card";
export default function App() {
const [pets, setPets] = useState( [] );
...
export default function App() {
const [pets, setPets] = useState( [] );
useEffect(
() => {
fetch( "http://jonippolito.net/teaching/mobile_applications/pet_finder/json/pets2.json" )
.then( response => response.json() )
.then( setPets );
}
}
);
import Card from "./components/Card";
export default function App() {
const [pets, setPets] = useState([]);
useEffect(() => {
...
}
return (
<View style={styles.container}>
<View style={styles.headerView}>
<Text style={styles.headerText}>Pet Finder</Text>
</View>
<ScrollView>{ renderPets() }</ScrollView>
</View>
);
}
const styles = StyleSheet.create({
export default function App() {
const [pets, setPets] = useState([]);
useEffect(() => {
...
}
const renderPets = () => {
return pets.map(
pet => (
<Card
petName={pet.petName}
petImage={pet.petImage}
petLink={pet.petLink}
/>
)
)
}
return (
<View style={styles.container}>
...
useEffect(
() => {
...
},
[pets] // Only re-render if this JSON has changed.
);
return pets.map(
pet => (
<Card
key={pet.id} // Must be unique to each record.
petName={ pet.petName }
...
/>
)
);
useEffect(
() => {
let didCancel = false;
if ( !didCancel ) {
fetch("http://jonippolito.net/teaching/mobile_applications/pet_finder/json/pets.json")
.then( response => response.json() )
.then( setPets );
};
return () => {
didCancel = true;
};
},
[pets]
);
export default class App extends React.component {
render() {
return(
...
)
}
}
export default function App() {
return(
...
)
}
Because Snacks are Web based, trying to fetch an external file without special headers can generate a CORS (Cross-Origin Resource Sharing) error. You can still simulate loading JSON by the following technique.
App.js
|_data
|_pets.js
export const pets =
[
{
"id": "grumpy_cat",
"petName": "Grumpy Cat",
"petImage": "https://s3.us-east-2.amazonaws.com/cat-demo-images/media/grumpy_thu.png",
"petLink": "https://en.wikipedia.org/wiki/Grumpy_Cat"
},
{
"id": "nyan_cat",
"petName": "Nyan Cat",
"petImage": "https://s3.us-east-2.amazonaws.com/cat-demo-images/media/nyan_cat_animated.gif",
"petLink": "https://en.wikipedia.org/wiki/Nyan_Cat"
},
...
]
import Card from "./components/Card";
export default function App() {
const [pets, setPets] = useState([]);
useEffect(() => {
...
}
return (
<View style={styles.container}>
<View style={styles.headerView}>
<Text style={styles.headerText}>Pet Finder</Text>
</View>
<ScrollView>{ renderPets() }</ScrollView>
</View>
);
}
const styles = StyleSheet.create({
export default function App() {
const [pets, setPets] = useState([]);
useEffect(() => {
...
}
const renderPets = () => {
return pets.map(
pet => (
<Card
petName={pet.petName}
petImage={pet.petImage}
petLink={pet.petLink}
/>
)
)
}
return (
<View style={styles.container}>
...
import Card from "./components/Card";
export default function App() {
const [pets, setPets] = useState([]);
useEffect(() => {
fetch(
"http://jonippolito.net/teaching/mobile_applications/pet_finder/json/pets2.json"
)
.then(response => response.json())
.then(setPets);
}, [pets]);
const renderPets = () => {
return pets.map(pet => (
<Card
petName={pet.petName}
petImage={pet.petImage}
petLink={pet.petLink}
/>
));
};
return (
<View style={styles.container}>
<View style={styles.headerView}>
<Text style={styles.headerText}>Pet Finder</Text>
</View>
<ScrollView>{renderPets()}</ScrollView>
</View>
);
}
const styles = StyleSheet.create({
...
import { pets } from "./data/pets.js" ;
export default function App() {
const renderPets = () => {
console.log("pets is " + pets.length) ; // Just a test to see if your JSON is loading.
return pets.map(pet => (
<Card
petName={pet.petName}
petImage={pet.petImage}
petLink={pet.petLink}
/>
));
...
}
Now your cards should show up with the individualized content from your JSON. 🏁