我试图通过将 Match 对象传递到我的react组件类来使用我的 url 作为参数。但是它不起作用!我在这里做错了什么?
当我将组件创建为 JavaScript 函数时,一切正常,但是当我尝试将组件创建为 JavaScript 类时,它不起作用。
也许我做错了什么?如何将 Match 对象传递给我的类组件,然后使用它来设置我的组件的状态?
我的代码:
import React, { Component } from 'react';
import axios from 'axios';
import PropTypes from 'prop-types';
class InstructorProfile extends Component {
constructor(props, {match}) {
super(props, {match});
this.state = {
instructors: [],
instructorID : match.params.instructorID
};
}
componentDidMount(){
axios.get(`/instructors`)
.then(response => {
this.setState({
instructors: response.data
});
})
.catch(error => {
console.log('Error fetching and parsing data', error);
});
}
render(){
return (
<div className="instructor-grid">
<div className="instructor-wrapper">
hi
</div>
</div>
);
}
}
export default InstructorProfile;