questions

[solved] Geting Syntax error: Unexpected token, expected “:” (25:13) while trying to get wordpress post with Axios in Reactjs

Rate this post

I’m trying to get a list of WordPress posts in React using Axios, I try importing the necessary files and I also install Axios with npm i Axios, but when I start the server I go an error Syntax error: Unexpected token, expected “:” (24:13) I write the below codes

import React, { Component } from "react";
import Intropage from "./components/intropage";
import "./customcss/style.css";
import axios from 'axios';

 
class Home extends React.Component {

    constructor( props ) {
        super(props);

        this.state = {
            loading: false,
            posts: [],
            error: ''
        }
    }

    componentDidMount() {
        const wordPressSiteUrl="http://localhost:8080/mysite_api";
        this.setState( state: { loading: true }, callback: () => {
            axios
            .get( url:`${wordPressSiteUrl}/wp-json/wp/v2/posts`)
                .then( onfulfilled: res => {
                this.setState( state: { loading: false, posts: res.data})
            })
        .catch( onrejected: error => this.setState( state: { loading: false, error: error.response.data }))
        });
    }
  
        render() {
          
    return (
      <>
  
      <Intropage />
            
      </>
    );
  }
}

export default Home;

I’m getting this error on the browser

Failed to compile
./src/home.js
Syntax error: Unexpected token, expected ":" (24:13)

  22 |         this.setState( state: { loading: true }, callback: () => {
  23 |             axios
> 24 |             .get( url:`${wordPressSiteUrl}/wp-json/wp/v2/posts`)
     |             ^
  25 |                 .then( onfulfilled: res => {
  26 |                 this.setState( state: { loading: false, posts: res.data})
  27 |             })


Click Here to see answer

Mazi

Mazi has nine years of web development experience with the past five years spent honing his skills with WordPress plugins and themes. He's an expert in WooCommerce and is the author of the Simple Sales Tax plugin, a popular sales tax compliance solution for WooCommerce. Brett understands the importance of excellent communication, prides himself on producing high-quality code, and is comfortable working solo or as part of a team.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button