Ad

How To Render State In ReactJS?

- 1 answer

I am a backend Python developer. But I need to make a simple front on the React. I send a request, I get a response, but I can’t get the state out.

class AppsList extends Component {
        state = {
            apps: []
        }

    componentDidMount() {
        axios.get('/apps')
          .then(function (response) {
            console.log(response);
            this.setState({
                apps: response.data
            })
          })
          .catch(function (error) {
            console.log(error);
          });
    }

    render() {
        return (
            <div>
                aaa
                <p>{this.state.apps}</p>
                aaa
            </div>
        );
     }
}

Response

enter image description here

Ad

Answer

The callback is not in arrow notation.

You can simply do:

.then(response => this.setState({ apps: response.data }))
Ad
source: stackoverflow.com
Ad