Reputation: 2116
I am trying to display results from an object in JSX. I have a api request in the componentWillMount() method. This returns an object like this
Object {Name: "testname", Surname: "testsurname", IdNumber: "9008067986743", Email: "[email protected]", Mobile: "+263 73 359 432"}
Now I need to display for eg. the Email in the JSX below
render(){
return (
<Container>
<Header />
<Content>
<Card>
<CardItem>
<Left>
<Thumbnail source={{uri: 'https://openclipart.org/image/2400px/svg_to_png/177482/ProfilePlaceholderSuit.png'}} />
<Body>
<Text>NEED EMAIL HERE</Text>
</Body>
</Left>
</CardItem>
</Card>
</Content>
</Container>
);
}
Please assist me with this, thank you
Upvotes: 3
Views: 9748
Reputation: 1517
After your API call, just put the response to the state, and plot it in your render method
constructor(props){
super(props);
this.state = { myObject: {} };
}
componentDidMount(){
//your API request here
.then(response => response.json().then(result => {
this.setState({ myObject: result });
}))
.catch((err) => { throw err; });
}
render(){
return (
<Container>
<Header />
<Content>
<Card>
<CardItem>
<Left>
<Thumbnail source={{uri: 'https://openclipart.org/image/2400px/svg_to_png/177482/ProfilePlaceholderSuit.png'}} />
<Body>
<Text>Email:</Text>
<Text>{this.state.myObject.Email}</Text>
<Text>Name:</Text>
<Text>{this.state.myObject.Name}</Text>
<Text>Surname:</Text>
<Text>{this.state.myObject.Surname}</Text>
<Text>ID Number:</Text>
<Text>{this.state.myObject.IdNumber}</Text>
</Body>
</Left>
</CardItem>
</Card>
</Content>
</Container>
);
}
Upvotes: 2
Reputation: 84
Assuming that you have your object in this.props.info
. You can do:
render(){
const { Email, Surname, Name } = this.props.info;
return (
<Container>
<Header />
<Content>
<Card>
<CardItem>
<Left>
<Thumbnail source={{uri: 'https://openclipart.org/image/2400px/svg_to_png/177482/ProfilePlaceholderSuit.png'}} />
<Body>
<Text>NEED EMAIL HERE</Text>
<Text>{Name}</Text>
<Text>{Surname}</Text>
<Text>{Email}</Text>
</Body>
</Left>
</CardItem>
</Card>
</Content>
</Container>
);
}
Upvotes: 0
Reputation: 1329
let requestedObject = {Name: "testname", Surname: "testsurname", IdNumber: "9008067986743", Email: "[email protected]", Mobile: "+263 73 359 432"}
render(){
return (
<Container>
<Header />
<Content>
<Card>
<CardItem>
<Left>
<Thumbnail source={{uri: 'https://openclipart.org/image/2400px/svg_to_png/177482/ProfilePlaceholderSuit.png'}} />
<Body>
<Text>{ requestedObject.email }</Text>
</Body>
</Left>
</CardItem>
</Card>
</Content>
</Container>
);
}
Whenever you write javascript in JSX(between tags like <Text> </Text>
) you need to put that javascript code in curly braces.
Upvotes: 0
Reputation: 8114
You need to set the data in your state initially to null, call API in componentWillMount and update state with the API response.
In following code initially state would be null so render function would return null. Now after API promise is completed your state would be updated by setState
and your render function
would be called again displaying your API data.
class Example extends React.Component {
state = {
data: null
}
componentWillMount () {
fetch(//your endpoint)
.then((data) => {
this.setState({data})
})
.catch((error) => {
console.warn(error);
})
}
render () {
if (!this.state.data) return null;
return (
<div>
<span>{this.state.data.email}</span>
</div>
)
}
}
Upvotes: 1