Reputation: 195
so in react I have an App component that is rendering several child components like this:
App
render() {
return (
//JSX inside
<BrowserRouter>
<div>
<Header />
<Switch>
<Route exact path="/" component={Courses} />
<Route exact path="/courses/create" component={() => <CreateCourse email={this.state.emailAddress} pass={this.state.password} />} />
<Route exact path="/courses/:id/update" component={() => <UpdateCourse email={this.state.emailAddress} pass={this.state.password} />} />
<Route exact path="/courses/:id" component={() => <CourseDetail email={this.state.emailAddress} pass={this.state.password} />} />
<Route exact path="/signin" component={ () => <UserSignIn signIn={this.signIn}/>} /> {/*pass in the signIn() in a prop called signIn to the UserSignIn component*/}
<Route exact path="/signup" component={UserSignUp} />
{/* <Route exact path="/signout" component={UserSignOut} /> */}
</Switch>
</div>
</BrowserRouter>
);
}
In this component I have params so that I am able to see a course by its id:
CourseDetail
componentDidMount() {
const {match: { params }} = this.props; //I used a code snippet from this video https://scotch.io/courses/using-react-router-4/route-params
//fetch data from API
axios
.get(`http://localhost:5000/api/courses/${params.id}`)
.then(results => {
//results param came back as data from api
this.setState({
//set state by setting the courses array to hold the data that came from results
course: results.data,
user: results.data.user
});
//console.log(results); //By console logging I was able to see that I am getting each individual course's info in the data object
});
}
//this method will be for deleting a course
handleDelete() {
const { match: { params }, history } = this.props;
axios.delete(`http://localhost:5000/api/courses/${params.id}`, {
auth: {
username: this.props.email,
password: this.props.pass
}
}).then(() => {
history.push("/"); //I used the history object and have it push to the homepage, that way every time I delete a course I am redirected to (/) afterwards
});
}
the error I am getting when I try to navigate to the CourseDetail component that uses params is:
can someone help?
Upvotes: 1
Views: 990
Reputation: 1219
Match is not defined because you didn't pass it down the component as props.
To do that
<Route exact path="/courses/:id/update" component={(routeProps) => <UpdateCourse email={this.state.emailAddress} pass={this.state.password} routeProps = {routeProps} />} />
you can then get your Match property via routeProps.
const {match} = this.routeProps;
Example,
<Route exact path="/courses/:id/update" component={(routeProps) => <UpdateCourse email={this.state.emailAddress} pass={this.state.password} {...routeProps} />} />
This is equivalent to writing-
<Route exact path="/courses/:id/update" component={(routeProps) => <UpdateCourse email={this.state.emailAddress} pass={this.state.password} Match = {this.routeProps.Match} Location = {this.routeProps.Location}/>} History = {this.routeProps.History />
Upvotes: 2
Reputation: 37775
You need to pass props like this read here
component={props => <CourseDetail {...props} email={this.state.emailAddress} pass={this.state.password} />} />
The props passed to courseDetails component do not have any prop name match
and in your componentDidMount you're doing this
const {match: { params }} = this.props;
Here match will be undefined so you can access params
You can understand by this example
let a = {a:{b:1}}
let {x:{b,}} = a
The above code is same as below
"use strict";
var a = {
a: {
b: 1
}
};
var b = a.x.b;
So eventually here if during destructuring if you don't have match as params you're trying to access
(this.props.match).params
|
|__________ This is undefined you end up `undefined.params`
Upvotes: 2
Reputation: 254
When you write this
const {match: { params }} = this.props;
It means you are expecting props to have a match attribute like below:
params = this.props.match.params;
This is why you are getting the specified error.
If you want to assign a variable the props use this
const params = props;
Note: [If you surround a variable with bracket const {match} = props; it means you are expecting a key match in props.
Upvotes: 0