Reactjs 无法访问ComponentDidMount方法内的道具
大家好,我是react js新手,当我尝试访问componentDidMount方法中的url时,我遇到了n个错误,如“无法正确读取道具”,请尝试修复我的错误 App.jsReactjs 无法访问ComponentDidMount方法内的道具,reactjs,api,axios,Reactjs,Api,Axios,大家好,我是react js新手,当我尝试访问componentDidMount方法中的url时,我遇到了n个错误,如“无法正确读取道具”,请尝试修复我的错误 App.js 从“React”导入React; 导入“/App.css”; 从“./行”导入行; 从“/Request”导入请求; 类应用程序扩展了React.Component{ 构造函数(){ 超级(); } render(){ 返回( 欢迎来到Netflix Clone ) } } 导出默认应用程序 Row.js这是我想要访问获取
从“React”导入React;
导入“/App.css”;
从“./行”导入行;
从“/Request”导入请求;
类应用程序扩展了React.Component{
构造函数(){
超级();
}
render(){
返回(
欢迎来到Netflix Clone
)
}
}
导出默认应用程序
Row.js这是我想要访问获取url的代码
从“React”导入React;
从“/Axios”导入Axios;
类行扩展了React.Component{
建造师(道具){
超级(道具);
此.state={
//电影:[],
}
}
componentDidMount(){
异步函数fetchData(){
const request=wait Axios.get(this.props.fetchUrl);
控制台日志(请求);
返回请求;
}
fetchData();
}
render(){
返回(
{this.props.title}
)
}
}
导出默认行;
我建议不要将componentDidMount与async一起使用,因为此方法属于react生命周期
相反,你可以这样做
import React from 'react';
import Axios from './Axios';
class Row extends React.Component {
constructor(props){
super(props);
this.state = {
// movie:[],
}
}
fetchData = async () => {
const request = await Axios.get(this.props.fetchUrl);
console.log(request);
return request;
}
componentDidMount(){
this.fetchData();
}
render() {
return (
<div>
<h3> {this.props.title}</h3>
</div>
)
}
}
export default Row;
从“React”导入React;
从“/Axios”导入Axios;
类行扩展了React.Component{
建造师(道具){
超级(道具);
此.state={
//电影:[],
}
}
fetchData=async()=>{
const request=wait Axios.get(this.props.fetchUrl);
控制台日志(请求);
返回请求;
}
componentDidMount(){
这是fetchData();
}
render(){
返回(
{this.props.title}
)
}
}
导出默认行;
import React from 'react';
import Axios from './Axios';
class Row extends React.Component {
constructor(props){
super(props);
this.state = {
// movie:[],
}
}
componentDidMount(){
async function fetchData(){
const request = await Axios.get(this.props.fetchUrl);
console.log(request);
return request;
}
fetchData();
}
render() {
return (
<div>
<h3> {this.props.title}</h3>
</div>
)
}
}
export default Row;
import React from 'react';
import Axios from './Axios';
class Row extends React.Component {
constructor(props){
super(props);
this.state = {
// movie:[],
}
}
fetchData = async () => {
const request = await Axios.get(this.props.fetchUrl);
console.log(request);
return request;
}
componentDidMount(){
this.fetchData();
}
render() {
return (
<div>
<h3> {this.props.title}</h3>
</div>
)
}
}
export default Row;