Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

react项目页面跳转/传值 #3

Open
machao07 opened this issue Feb 17, 2022 · 0 comments
Open

react项目页面跳转/传值 #3

machao07 opened this issue Feb 17, 2022 · 0 comments

Comments

@machao07
Copy link
Owner

一、跳转

1、history.push 跳转

  • 引用 withRouter
  • 导出组件时使用
import { withRouter } from 'react-router-dom'

class Header extends React.Component {

}
export default withRouter(Header);

2、NavLink跳转

import { NavLink } from "react-router-dom";
<NavLink to="/modify">修改密码</NavLink>

二、传值

1、路由传值 query/params

this.props.history.push({
    pathname: 'login',
    query: {
        id: 3231,
        name: 3231
    }
})
this.props.history.push('modify')

2、路由传值获取参数

this.props.match.query.id // 3231
this.props.match.params.name // 3231

image

3、props传值

父组件:

state = {
    collapsed: false,
};
render() {
    const { collapsed } = this.state;
    return (
        <Slider collapsed={collapsed}/>
    )
}

子组件:

componentDidMount(){
    // console.log(this.props)
    const { pathname } = this.props.location

    if( pathname ) {
        this.setState({               
            selectedKeys:this.props.location.pathname
        })
    }
}

console.log(this.props)

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant