最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Display results from object in JSX - Stack Overflow

programmeradmin1浏览0评论

I am trying to display results from an object in JSX. I have a api request in the ponentWillMount() 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: '.png'}} />
              <Body>
                <Text>NEED EMAIL HERE</Text>
              </Body>
            </Left>
          </CardItem>
        </Card>            
      </Content>
    </Container>
  );
}

Please assist me with this, thank you

I am trying to display results from an object in JSX. I have a api request in the ponentWillMount() 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/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

Share Improve this question asked Nov 2, 2017 at 10:24 RRBRRB 2,1169 gold badges48 silver badges84 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 2

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: {} };
}

ponentDidMount(){
  //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/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>
  );
}

You need to set the data in your state initially to null, call API in ponentWillMount 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 pleted 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
  }

  ponentWillMount () {
    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>
    )
  }
}
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/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.

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/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>
          );
        }
发布评论

评论列表(0)

  1. 暂无评论