当前位置: 首页 > 知识库问答 >
问题:

React路由器domv5。2、无法从父组件接收的道具将道具传递给子组件

漆雕伟志
2023-03-14

我正在使用react路由器dom v5。反应v16中的2.0。13.1项目中,我使用静态路由将道具从父组件传递到子组件,父组件从其父组件(即祖父)接收道具。从应用程序。js-

class AppLayout extends Component{
  constructor(props){
    super(props);
  }
  render(){
    console.log('Applayout'+' '+ this.props.data[1]);
      return(
          <Router>
              <div>
                  <Navbar/>
                  <div className={classes.main}>
                      <Sidebar/>
                      <div className = {classes.pages}>
                          <Route path='/' exact component={Dashboard}/>
                          <Route path='/toolsandutils' exact component={ToolsandUtils}/>
                          <Route path='/failures' component={Failures}/>
                          <Route path='/osshare' component={OSshare}/>
                          <Route path='/employee' component={Employee}/>
                          <Route path='/apidata' render={props=> <APIData data={this.props.data}/>}/>
                      </div>
                  </div>                
              </div>
          </Router>
                         
              
      )
  }
}```

共有1个答案

杭英杰
2023-03-14

我也有同样的问题。我尝试了很多不同的方法,但都没有成功。在路径组件中,我试图传递的道具将是未定义的。

我最近确实更新了我使用的babel版本。所以,这可能与使用旧的巴别塔版本有关。

使用这个结构对我很有效:

<Router>
  <Switch>
    <Route
      exact
      path="/admin/clients"
      render={() =>
        <ClientsRoute
          onSaveClient={this.handleSaveClient}
          onActivateExistingClient={this.handleActivateExistingClient}
        />
      }
    />
    <Route
      exact
      path="/admin/clients/:page(\d+)"
      render={() =>
        <ClientsPagedRoute
          onSaveClient={this.handleSaveClient}
          onActivateExistingClient={this.handleActivateExistingClient}
        />
      }
    />
  </Switch>
</Router>

我把这个打包了。之前的json:

{
  "devDependencies": {
    "babel-core": "^6.26.3",
    "babel-loader": "^7.1.4",
    "babel-plugin-transform-class-properties": "^6.24.1",
    "babel-plugin-transform-object-rest-spread": "^6.26.0",
    "babel-polyfill": "^6.26.0",
    "babel-preset-env": "^1.7.0",
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-react": "^6.24.1"
  }
}

现在我有package.json:

{
  "devDependencies": {
    "@babel/core": "^7.11.6",
    "@babel/plugin-proposal-class-properties": "^7.10.4",
    "@babel/preset-env": "^7.11.5",
    "@babel/preset-react": "^7.10.4",
    "babel-loader": "^8.1.0"
  },
  "dependencies": {
    "core-js": "^3.6.5",
    "regenerator-runtime": "^0.13.7"
  }
}

我有这个。法律改革委员会:

{
  "presets": ["env", "react"],
  "plugins": ["transform-class-properties", "transform-object-rest-spread"]
}

现在我有了这个。法律改革委员会:

{
  "presets": ["@babel/preset-env", "@babel/preset-react"],
  "plugins": ["@babel/plugin-proposal-class-properties"]
}
 类似资料:
  • 我无法使用反应路由器通过道具。我到现在为止的代码: 错误截图 我正试图访问控制台中的id,就这么简单。 错误仅在我尝试传递道具时显示 源链接:https://reacttraining.com/react-router/web/api/Route/route-props

  • 我有一个模态,我想在其中放置一个动态嵌套视图。我认为这样做的一个好方法是像这样使用 我想把一些道具传给这些组件,就像我平时在React中一样 最终,我将移动到存储/获取状态中的这些属性,但是考虑到当前的实现,在定义开关

  • 我使用的反应与反应路由器的项目,我需要传递道具的子组件从路由。 我有这样的设置: App.js Home.js 问题是主组件总是给出错误“未定义”。我无法理解如何解决这个问题。如果我从Home.js中移除道具,组件渲染就很好。但当我尝试访问道具时,会出现错误。

  • 问题内容: 在组件中,我需要将函数的返回值传递给属性。然后从属性需要被传递到的组件。新来的反应。不确定如何将属性从子级传递到组件。 问题答案: 您可以将函数从父级传递给子级,子级可以使用颜色调用该函数(很多操作都类似于事件处理程序)。在App中收到颜色后,使用.setState()将其分配给状态值,然后将其在render()中获取

  • 问题内容: 我需要使用路由器将道具传递到组件。这是我的代码: 如您所见,isAuthenticated我想传递给Login组件的道具。 当我登录道具时, isAuthenticated 道具不存在。我做错了什么?如何正确通过道具?我关注了文档以及其他讨论。据我了解,它应该可以工作。的版本 做出反应路由器 和 反应路由器- DOM 是 4.0.0 问题答案: 像这样传递它: 它应该在“登录组件”中可

  • 我对反应和试图理解如何传递道具很陌生。 首先,我设置了一个Navlink组件,该组件包含子类别属性: ,,都是从json文件中获取的,所以这部分是可以的。 每条路线的定义如下: 所以它呈现一个名为