1

Sending request to API and get API response in console and that return data will be object .

API RESPONSE:

Refer Here :Json API response

Now how to disply data on webpage using axios. Below code shows how to call API

import React, { Component } from 'react'
import axios from 'axios'

class PostForm extends Component {
    constructor(props) {
        super(props)

        this.state = {
            key: '10-10-21',
            // Where data will be saved.
            data: [],
        }
        console.log(this.state)
    }

    changeHandler = e => {
        this.setState({ [e.target.name]: e.target.value })
    }

    submitHandler = e => {
        e.preventDefault()
        
        axios
        .get(`http://127.0.0.1:8000/hgetall_hash?key=${this.state.key}`)
        .then(response => {
                        // Updating the state to trigger a re-render       
            this.setState({data: response.data});
            console.log(response.data)
        })
        .catch(error => {
            console.log(error)
        })
    }

    render() {
        const { key } = this.state
        
        return (
            <center><div>
                <form onSubmit={this.submitHandler}>
                    <div>
                        <h2> DATE PICKER</h2><br></br>
                        <input
                            type="text"
                            name="key"
                            value={key}
                            onChange={this.changeHandler}
                        />                        
                    </div>
                    <br></br>
                    <button type="submit">Submit</button>
                </form>
            </div></center>
        )
    }
}
export default PostForm

From this code to to store response in variable and map , display data in Grid view.

3
  • 2
    you can use {this.state.data.map(item => {map your item here, don't forget the key}} somewhere in your returned jsx Commented Oct 14, 2021 at 5:10
  • also, the <center> tag is deprecated in HTML5 and the <br /> tag is self closing. Finally, your change handler needs to be rebound to "this" in the constructor Commented Oct 14, 2021 at 5:11
  • @DovRine can you explain with my code i want to fetch time and video on display Commented Oct 14, 2021 at 16:19

1 Answer 1

1

The response will be stored in the data which you have defined inside this.state.

this.state.data.map((item)=>( {item.time} )) to display is grid you can use display: grid
Sign up to request clarification or add additional context in comments.

7 Comments

But i got some error when i try this
can you show the error
TypeError: this.state.data.map is not a function
Can you please share the screenshot od the code which is giving you error, I believe there is a syntax error
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.