javascript - 无法通过 React Router 传递 props

标签 javascript reactjs react-props

我有类 GetProfile 扩展了 React.Component 并在渲染中返回状态。我想用 React Router 获取 props GetProfile 但我收到消息

Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.

class GetProfile extends React.Component{
....
render(){
        return(
            <div>
                {React.cloneElement(this.props.children, { 
                        data: this.state.data, 
                        department : this.state.department 
                    })
                }
            </div>
        )
    }
}
export default GetProfile;

在 Router.js 中这是我的代码

import Home from './components/Home';
import GetProfile from './action/GetProfile';

....
<GetProfile>
   <Authenticated exact path="/home" render={(props) => <Home {...props.data}/>}/>
</GetProfile>
....

最佳答案

try by importing React
import React from 'react'
class GetProfile extends React.Component{
    ....
    render(){
        return(
            <div>
                {React.cloneElement(this.props.children, { 
                    data: this.state.data, 
                    department : this.state.department 
                })
            }
        </div>
    )
  }
 }
 export default GetProfile;

关于javascript - 无法通过 React Router 传递 props,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55233428/

相关文章:

javascript - 将父组件中的: Pass image from this.状态 react 到子组件

javascript - 更改 Zombie.js 的版本

javascript - Javascript 中的 object.create 语法用法

javascript - 我可以在 AWS Amplify 注册表单上添加选择输入类型吗?

reactjs - 如何通过react-fontawesome使用图层

javascript - 在 React.js 中,我的状态不是在第一次单击时更新,而是在第二次单击时更新

javascript - 以编程方式确定 SVG 路径生成的形状

javascript - 使 javascript 数组中的值之间的距离彼此相等

javascript - 如何绑定(bind)函数而不是创建新函数?

javascript - 如何根据属性正确处理订阅