分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 前端开发

react-router JS 控制路由跳转(转载)

发布时间:2023-09-06 01:42责任编辑:沈小雨关键词:暂无标签

Link组件用于正常的用户点击跳转,但是有时还需要表单跳转、点击按钮跳转等操作。这些情况怎么跟React Router对接呢?

下面是一个表单。

<form onSubmit={this.handleSubmit}> ?<input type="text" placeholder="userName"/> ?<input type="text" placeholder="repo"/> ?<button type="submit">Go</button></form>

第一种方法是使用browserHistory.push

import { browserHistory } from ‘react-router‘// ... ?handleSubmit(event) { ???event.preventDefault() ???const userName = event.target.elements[0].value ???const repo = event.target.elements[1].value ???const path = `/repos/${userName}/${repo}` ???browserHistory.push(path) ?},

第二种方法是使用context对象。

export default React.createClass({ ?// ask for `router` from context ?contextTypes: { ???router: React.PropTypes.object ?}, ?handleSubmit(event) { ???// ... ???this.context.router.push(path) ?},})

react-router JS 控制路由跳转(转载)

原文地址:https://www.cnblogs.com/daiwenru/p/8444357.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved