【问题标题】:Serverside redirect onClick with react-router使用 react-router 的服务器端重定向 onClick
【发布时间】:2019-02-03 15:19:08
【问题描述】:

onClick 用于服务器端呈现的组件时,如何执行Redirect?以下是我拥有的组件。当我单击该按钮时,我的 console.log 会显示,但我没有被重定向到另一个页面。

class UserNav extends React.Component {
  constructor(props) {
    super(props);
    this.signUp = this.signUp.bind(this);
  }

  signUp() {
    console.log('here');
    return <Redirect to="/signup" push />;
  }

  render() {
    const view = this.props.authentication ? (
      <div>test</div>
    ) : (
      <div>
        <button>Login</button>
        <button onClick={this.signUp}>Sign Up</button>
      </div>
    );
    return (<div>{ view }</div>
    );
  }
}

【问题讨论】:

    标签: javascript reactjs ecmascript-6 react-router server-side-rendering


    【解决方案1】:

    利用道具。

    this.props.history.push('/signup');
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-06
      • 2016-05-24
      • 2018-08-31
      • 1970-01-01
      • 1970-01-01
      • 2016-03-26
      相关资源
      最近更新 更多