简体   繁体   English

React组件接收未定义的道具

[英]React component receiveing props as undefined

I build a project for a school's records system, in which I build the front-end with React. 我为学校的记录系统构建了一个项目,在其中我使用React构建了前端。 On the main component of the admin page, I wanted to have a react-router which will navigate through the admin dialogs. 在管理页面的主要组件上,我希望有一个react-router,它将在管理对话框中导航。 As I tried to implement this, the following problem occurred: when trying to pass parameters to a class through the react route component, the child component receives no props. 当我尝试实现此功能时,发生了以下问题:当尝试通过react route组件将参数传递给类时,子组件没有收到任何支持。

I have the following react component hierarchy: 我有以下反应组件层次结构:

class Test extends React.Component {
   constructor() {
       super();

       console.log("in class: " + this.props)
   }

   render() { return <div>test</div>}
}

class AdminPage extends BasicPage {
    /* Other class functions here... */
    render() {
       let pageBody = "";
       if(!this.state.isLoading)
           pageBody = (
               <Router>
                   <Switch>
                       <Route path={"/:schoolName/admin"} component={AdminMenu} exact/>
                       <Route path={"/:schoolName/admin/view/:id"} exact
                          component={() => <Test par1="abc" />} />
                   </Switch>
               </Router>
           );
       return (
           <Layout title={ this.state.isLoading ?
               TITLE_UNTIL_LOADED :
               PAGE_TITLE + this.state.schoolPrefs.heb_name}
                   subtitle={ this.state.subtitle }
                   notification={ this.state.notification }
                   isLoading={ this.state.isLoading }
           >
               {pageBody}
           </Layout>
       );
    }
}

When I go to /Random Name/admin/view/someID , it prints to the console in class: undefined . 当我转到/Random Name/admin/view/someID ,它将in class: undefined打印到控制台。

I then wanted to see if the problem is in the passing component or the receiving one. 然后,我想看看问题出在传递组件还是接收组件。 I defined the function otherTest(props) as follows: 我定义了函数otherTest(props)如下:

function otherTest(props) {
   console.log("Function props: " + props);
   return (<Test {...props} />);
}

And then changed the route component like so: 然后更改路由组件,如下所示:

<Route path={"/:schoolName/admin/view/:id"} exact
  component={otherTest} />

When then I went to /Random Name/admin/view/someID , I saw that the function received the props just fine, but the log within <Test … /> still printed undefined . 然后当我去/Random Name/admin/view/someID ,我看到函数收到的道具就好了,但日志中<Test … />上还印着undefined

I also tried adding <Test param1=”123” /> after the {pageBody} variable in the main render function, but it printed in class: undefined as well. 我还尝试在主渲染函数中的{pageBody}变量之后添加<Test param1=”123” /> ,但该变量也打印in class: undefined中。

Does someone know where the problem might be? 有人知道问题可能在哪里吗?

Thanks. 谢谢。

You must take props parameter from constructor and then pass it to super. 您必须从构造函数中获取props参数,然后将其传递给super。

constructor(props){
super(props);
}

Do not use this.props in constructor beacuse constructor fire only at the creating class moment. 不要在构造函数中使用this.props,因为构造函数仅在创建类时才触发。 Use this code: 使用此代码:

constructor(props) {
 super(props);

 console.log(props);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM