反应是不认识参数?

时间:2019-03-16 04:33:07

标签: javascript reactjs react-router react-router-dom

因此在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组件时遇到的错误是:

CourseDetail error

有人可以帮忙吗?

3 个答案:

答案 0 :(得分:2)

您需要传递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`

答案 1 :(得分:2)

未定义

匹配,因为您没有将其作为道具传递给组件。

要这样做

<Route exact path="/courses/:id/update" component={(routeProps) => <UpdateCourse email={this.state.emailAddress} pass={this.state.password} routeProps = {routeProps} />} />

您可以通过以下方式获取Match属性 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 />

答案 2 :(得分:0)

写这篇文章

const {match:{params}} = this.props;

这意味着您期望道具具有 match 属性,如下所示:

params = this.props.match.params;

这就是为什么您得到指定的错误。

如果您想分配一个变量,道具就可以使用

const params = props;

注意:[如果用括号 const {match} = props包围变量,表示您期望props中的键匹配。