材质 UI 按钮未重定向到路径

我有一个按钮,它具有onClick功能,可以重定向到带有道具的某个组件


<Button

  variant="contained"

  color="primary"

  className={classes.button}

  endIcon={<SendIcon/>}

  onClick={() => {

        <Redirect 

         to={{

          pathname: '/view',

           state: {

            id: row.id

                  }

             }}

          />

         }}

 >

    View History

 </Button>

我的路由代码


<Route exact path="/view"  render={(props) => <NewTestComp {...props}/>} />

按钮没有重定向,我尝试了许多不同的解决方案,但我找不到答案


翻阅古今
浏览 70回答 1
1回答

拉丁的传说

您需要利用 重定向到新页面,并且需要呈现组件才能有效,并且 onClick 处理程序不会呈现返回的值history.pushRedirect<Button&nbsp; variant="contained"&nbsp; color="primary"&nbsp; className={classes.button}&nbsp; endIcon={<SendIcon/>}&nbsp; onClick={() => {&nbsp; &nbsp; &nbsp; &nbsp; props.history.push({&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pathname: '/view',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;state: {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; id: row.id&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; }&nbsp;>&nbsp; &nbsp; View History&nbsp;</Button>使用方式和Redirect would be to setStaterender the component conditionally<Button&nbsp; variant="contained"&nbsp; color="primary"&nbsp; className={classes.button}&nbsp; endIcon={<SendIcon/>}&nbsp; onClick={() => {&nbsp; &nbsp; &nbsp; &nbsp; this.setState({ redirectWithState: {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; id: row.id&nbsp; &nbsp; &nbsp; &nbsp; }})&nbsp;>&nbsp; &nbsp; View History&nbsp;</Button>{ redirectWithState && <Redirect&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;to={{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pathname: '/view',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;state: redirectWithState&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript