反应不识别参数?
react is not recognizing params?
所以在 React 中,我有一个 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>
);
}
在这个组件中我有参数,所以我可以通过它的 id 查看课程:
课程详情
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
});
}
当我尝试导航到使用参数的 CourseDetail 组件时遇到的错误是:
有人可以帮忙吗?
你需要像这样传递道具read here
component={props => <CourseDetail {...props} email={this.state.emailAddress} pass={this.state.password} />} />
传递给 courseDetails 组件的道具没有任何道具名称 match
并且在您的 componentDidMount 中您正在这样做
const {match: { params }} = this.props;
此处匹配未定义,因此您可以访问 params
看这个例子就明白了
let a = {a:{b:1}}
let {x:{b,}} = a
以上代码同下
"use strict";
var a = {
a: {
b: 1
}
};
var b = a.x.b;
所以最终在这里,如果在解构过程中,如果你没有匹配作为你试图访问的参数
(this.props.match).params
|
|__________ This is undefined you end up `undefined.params`
当你写这篇文章时
常量{匹配:{参数}}=this.props;
这意味着您希望道具具有如下 匹配 属性:
params = this.props.match.params;
这就是您收到指定错误的原因。
如果你想分配一个变量,道具使用这个
const params = props;
注意:[如果你用方括号括起一个变量 const {match} = props; 这意味着你期望在 props 中进行键匹配。
Match 未定义,因为您没有将其作为道具传递给组件。
这样做
<Route exact path="/courses/:id/update" component={(routeProps) => <UpdateCourse email={this.state.emailAddress} pass={this.state.password} routeProps = {routeProps} />} />
然后您可以通过以下方式获得匹配 属性
routeProps.
const {match} = this.routeProps;
或者简单地使用 属性 扩展符号 它将 routeProps 中的属性作为组件中的离散属性展开。
示例,
<Route exact path="/courses/:id/update" component={(routeProps) => <UpdateCourse email={this.state.emailAddress} pass={this.state.password} {...routeProps} />} />
这相当于写-
<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 />
所以在 React 中,我有一个 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>
);
}
在这个组件中我有参数,所以我可以通过它的 id 查看课程:
课程详情
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
});
}
当我尝试导航到使用参数的 CourseDetail 组件时遇到的错误是:
有人可以帮忙吗?
你需要像这样传递道具read here
component={props => <CourseDetail {...props} email={this.state.emailAddress} pass={this.state.password} />} />
传递给 courseDetails 组件的道具没有任何道具名称 match
并且在您的 componentDidMount 中您正在这样做
const {match: { params }} = this.props;
此处匹配未定义,因此您可以访问 params
看这个例子就明白了
let a = {a:{b:1}}
let {x:{b,}} = a
以上代码同下
"use strict";
var a = {
a: {
b: 1
}
};
var b = a.x.b;
所以最终在这里,如果在解构过程中,如果你没有匹配作为你试图访问的参数
(this.props.match).params
|
|__________ This is undefined you end up `undefined.params`
当你写这篇文章时
常量{匹配:{参数}}=this.props;
这意味着您希望道具具有如下 匹配 属性:
params = this.props.match.params;
这就是您收到指定错误的原因。
如果你想分配一个变量,道具使用这个
const params = props;
注意:[如果你用方括号括起一个变量 const {match} = props; 这意味着你期望在 props 中进行键匹配。
Match 未定义,因为您没有将其作为道具传递给组件。
这样做
<Route exact path="/courses/:id/update" component={(routeProps) => <UpdateCourse email={this.state.emailAddress} pass={this.state.password} routeProps = {routeProps} />} />
然后您可以通过以下方式获得匹配 属性 routeProps.
const {match} = this.routeProps;
或者简单地使用 属性 扩展符号 它将 routeProps 中的属性作为组件中的离散属性展开。
示例,
<Route exact path="/courses/:id/update" component={(routeProps) => <UpdateCourse email={this.state.emailAddress} pass={this.state.password} {...routeProps} />} />
这相当于写-
<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 />