[React] Redirect


1.素子を通して直接
function Navigation() {
    return (
    <div className="nav">
        <Link to="/">Home</Link>
        <Link to={{
            pathname:"/about",
            state: {
                fromNavigation: true
            }
        }}>About</Link>
    </div>
    );
}
2.history props経由のRedirect
  • に渡された道具がundefinedの場合、直接家に戻るコード
  • class Detail extends React.Component{
        componentDidMount(){
            const { location, history } = this.props;
            if(location.state === undefined) {
                history.push("/");
            }
        }
        render(){
            return <span>hello</span>
        }
    }