questions
[solved] Geting Syntax error: Unexpected token, expected “:” (25:13) while trying to get wordpress post with Axios in Reactjs
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