Reactjs 组件没有';我不接受道具

Reactjs 组件没有';我不接受道具,reactjs,Reactjs,在myhome组件中,我读取用户名,然后检查其是否有效(如果用户名正确,axios将返回一个用户)。如果有效,我单击要显示仪表板组件的按钮。问题是,即使名称有效,Dashboard也不会从主页组件接收道具。我用componentDidUpdate尝试了一些东西,但效果不太好。我不明白的是,为什么如果我输入了一个正确的用户,我的仪表板会被渲染,但却没有得到道具 这是我的主页组件,没有构造函数: inputChange(event) { this.setState({username:eve

在myhome组件中,我读取用户名,然后检查其是否有效(如果用户名正确,axios将返回一个用户)。如果有效,我单击要显示仪表板组件的按钮。问题是,即使名称有效,Dashboard也不会从主页组件接收道具。我用componentDidUpdate尝试了一些东西,但效果不太好。我不明白的是,为什么如果我输入了一个正确的用户,我的仪表板会被渲染,但却没有得到道具

这是我的主页组件,没有构造函数:

inputChange(event) {
    this.setState({username:event.target.value});
}

login() {
    this.setState({clicked: true})
    UsersService.validUser(this.state.username).then((user) => {
        this.setState({valid: true, user: user})
    });
}

change() {
    this.setState({clicked: false})
}

render() {
    if(this.state.clicked) {
        if (this.state.valid)
            return (
                <Redirect to={{ pathname: '/dashboard', state: { user: this.state.user } }}/>
            )
        else
            this.change;
    }
    return (
        <div>
            <input onChange={this.inputChange.bind(this)}/>
            <button onClick={this.login}>LOGIN</button>
        </div>
}
inputChange(事件){
this.setState({username:event.target.value});
}
登录(){
this.setState({单击:true})
UsersService.validUser(this.state.username)。然后((user)=>{
this.setState({valid:true,user:user})
});
}
更改(){
this.setState({单击:false})
}
render(){
if(this.state.clicked){
if(this.state.valid)
返回(
)
其他的
这就是变化;
}
返回(
登录
}
仪表板组件

export interface DashboardProps {
    user: IUser
}

interface DashboardState {
}

class Dashboard extends React.Component<DashboardProps, DashboardState> {
    public static defaultProps: DashboardProps = {
        user: {
            userId: '',
            name: '',
            phone: '',
            email: '',
            age: '',
            tag: []
        }
    }
    constructor(props: DashboardProps) {
        super(props);

        this.state = {
        };
    }

    render() {
        return (
            <div>
                <Header user={this.props.user}/>
                <div>
                    <span>Hi, {this.props.user.name}</span> <br/>
                </div>
            </div>
        );
    }
}

export default withRouter(Dashboard);
导出界面仪表板道具{
用户:IUser
}
接口仪表板状态{
}
类Dashboard扩展了React.Component{
公共静态默认道具:仪表板道具={
用户:{
用户标识:“”,
名称:“”,
电话:'',
电子邮件:“”,
年龄:'',
标签:[]
}
}
构造函数(道具:仪表板道具){
超级(道具);
此.state={
};
}
render(){
返回(
您好,{this.props.user.name}
); } } 使用路由器导出默认值(仪表板);
尝试在仪表板组件中使用
路由器的
HOC,当前路由道具将注入组件

export default withRouter(Dashboard);
编辑:


您的状态信息将在
this.props.location.state.user

@zeudarius上提供-您可以添加有问题的
仪表板组件吗?我已经添加了仪表板组件您的回答是正确的,我只需添加
键入DashboardPropsType=DashboardProps&RouteComponentProps;
即可使用props.location谢谢!