How to get the json data with react, fetch and rest api?

0

Hello, I have a mortal doubt .. I am doing some studies with React, and for this I am pulling a json from the rest api wordpress.

When I put something like fetch (" link ) when I step querys like fetch (" link ") it does not pull the data.

Could anyone help me?

    
asked by anonymous 13.09.2018 / 16:24

1 answer

0

import React, { Component } from 'react';
import {
    BrowserRouter as Router,
    Route,
    Link
  } from 'react-router-dom';

class Post extends React.Component {
    constructor() {
        super();

        this.state = {
            post: {
                title: {
                    rendered: ''
                },
                content: {
                    rendered: ''
                }
            }
        }
    }
    componentDidMount() {
        var url  = window.location.href;
        var slug = url.split("/")[url.split("/").length -1];
        fetch("https://pulsemaker.com.br/wp-json/wp/v2/posts/179")
            .then(results => results.json())
            .then(results => this.setState({'post': results}));
    }
    render() {
        return (
            <div className="wrapper">
               <h1>{this.state.post.title.rendered}</h1>
                <article dangerouslySetInnerHTML={{ __html: this.state.post.content.rendered }} />
            </div>
        );
    }
}
export default Post
    
13.09.2018 / 16:51