[英]I am trying to fetch json data from Rest API into react, but it showing some error
I am trying to fetch the data from Rest API "TVMaze". 我正在尝试从Rest API“ TVMaze”获取数据。 the data is in json format. 数据为json格式。 I am using react.js for this 我为此使用react.js
this is my myclass.js
file 这是我的myclass.js
文件
import React from "react";
import ReactDOM from "react-dom";
import axios from "axios";
function Myclass(){
return(
axios.get("http://api.tvmaze.com/schedule?country=US&date=2019-05-01")
.then (function(response){
console.log(response);
});
);
}
export default Myclass;
This is my index.js
file: 这是我的index.js
文件:
import React from "react";
import ReactDOM from "react-dom";
import Myclass from "./myclass.js";
import "./styles.css";
function App() {
return (
<Myclass/>
);
}
const rootElement = document.getElementById("root");
ReactDOM.render(<App/>, rootElement);
This is the error message that I am getting: 这是我收到的错误消息:
SyntaxError: /src/myclass.js: Unexpected token, expected "," (10:6)
src/myclass.js: Unexpected token, expected "," (10:6) 8 | .then (function(response){ 9 | console.log(response); > 10 | }); | ^ 11 | ); 12 | } 13 | 8 | .then (function(response){ 9 | console.log(response);
> 10 | }); | ^ 11 | ); 12 | } 13 |
browser
Parsing error: Unexpected token, expected "," 8 | .then (function(response){ 9 | console.log(response); > 10 | }); | ^ 11 | ); 12 | } 13 | (null)
eslint
There are several issues in your code. 您的代码中有几个问题。 A react component must either return something to display (eg: dom elements) or null
but here you return a Promise
when you write react组件必须返回要显示的内容(例如dom元素)或为null
但是在此处您编写时会返回Promise
return(
axios.get("http://api.tvmaze.com/schedule?country=US&date=2019-05-01")
.then (function(response){
console.log(response);
});
);
Plus you have a space between then
and function
which raises the error. 另外,您在then
和function
之间还有一个空格,这会引发错误。
When you want to show data coming from an API request, it's good practice to call your API from within componentDidMount
and store your data in the state
of your component. 当您想显示来自API请求的数据时,最好在componentDidMount
内调用API并将数据存储在componentDidMount
的state
中。
You can do it with class-based component: 您可以使用基于类的组件来做到这一点:
import React from "react";
import axios from "axios";
class Myclass extends React.Component{
state = {data: null};
componentDidMount(){
axios.get("http://api.tvmaze.com/schedule?country=US&date=2019-05-01")
.then(function(response){
this.setState({data: response})
});
}
render(){
if(!this.state.data){
return null; //or a loading spinner or whatever
}
return data // here depending on the format of the data (array or object) you might need to do some process.
}
}
export default Myclass;
Or with hooks: 或带钩子:
import React, {useEffect} from "react";
import axios from "axios";
const MyClass = function(){
const [data, setData] = useState(null);
useEffect(() => {
axios.get("http://api.tvmaze.com/schedule?country=US&date=2019-05-01")
.then(function(response){
setDate(response)
});
}, []) // with an empty array as a dependency, the API call will be run only once when the component is mounted.
if(!data){
return null;
}
return data // same comment than before to show your data properly
}
Hope it helps. 希望能帮助到你。
Your myclass component should look as follows. 您的myclass组件应如下所示。
import React ,{useEffect} from "react";
import ReactDOM from "react-dom";
import axios from "axios";
function Myclass(){
// The below useEffect is equivalent to a componentDidMount.
useEffect(()=>{
axios.get("http://api.tvmaze.com/schedule?country=US&date=2019-05-01")
.then (function(response){
console.log(response);
});
},[]);
return(
);
}
export default Myclass;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.