javascript - 链接到路由中的反应路由器和打字稿“匹配”问题
问题描述
由于我是 react 和 typescript 的新手,我已经阅读了几篇关于如何使用 typescript 从 react-router 中获取“匹配”属性的文章,但我无法成功地做到这一点。我没有传递任何参数,我只想能够使用 react-router 的 match.url。
所以我看到的例子是 login.tsx 文件中的字段应该使用 {match.url}。我在这里尝试了 react 4 路由器演示:https ://codesandbox.io/s/nn8x24vm60?from-embed where they used { ${match.url}/register
} 但这也不起作用。然后我看到一些帖子,人们说你必须声明一个接口,但它在 RouteComponent 和 Route 之间拆分,并且大多数都在处理参数。基本上我想要它做的就是当我单击链接切换到注册路由时(它在 register.tsx 中——这里没有显示,因为它是一个简单的文件,上面只有一个标题)。
目前它抛出以下错误:
"Error TS2739 (TS) Type '{}' is missing the following properties from type
'Readonly<MyProps & RouteComponentProps<{}, StaticContext, any>>': register, history, location, match"
任何关于我做错了什么的帮助将不胜感激。
app.tsx 文件:
import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import { BrowserRouter as Router, Redirect, Link, Route, Switch } from 'react-router-dom';
import { Home } from './home';
import { Register } from './register';
import { NavBar } from './navbar';
export class App extends React.Component<{}, {}> {
render() {
return (
<Router>
<div>
<NavBar/>
<Switch>
<Route path="/" component={Home} />
<Route path="/register" component={Register} />
</Switch>
</div>
</Router>
);
}
}
ReactDOM.render(, document.getElementById('root'));
home.tsx 文件:
import React from 'react';
import { Login } from './login';
import { Jumbotron } from 'react-bootstrap';
const jumboStyle = {
background: 'lightgray',
height: '20%',
width: '40%',
margin: 'auto'
};
export class Home extends React.Component<{}, {}> {
render() {
return (
<div>
< Jumbotron style={jumboStyle}>
<h1>Welcome to the new League!</h1>
<h4>Please log in with your username and password to continue</h4>
<Login />
<br />
<br />
</Jumbotron>
</div>
);
}
}
login.tsx 文件:
import React from 'react';
import { Link, RouteComponentProps } from "react-router-dom";
import TextField from '@material-ui/core/TextField';
import Button from '@material-ui/core/Button';
import { Alert } from 'react-bootstrap';
import { Register } from './register';
interface IState {
[key: string]: any; // or the type of your input
}
interface MyProps {
register: Register
}
const styles = {
background: 'lightblue'
};
export class Login extends React.Component<MyProps & RouteComponentProps, IState> {
constructor(props) {
super(props);
this.state = {
username: '',
password: '',
authorized: false
};
this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}
handleChange(event) {
const target = event.target;
const value = target.value;
const name = target.name;
this.setState({
[name]:value
});
}
handleSubmit(event) {
//we are goingto submit the form to the database
event.prevent.default();
}
render() {
return (
<div>
<form noValidate autoComplete="off" onSubmit={this.handleSubmit} style={{ textAlign: 'center' }}>
<TextField
id="username"
name="username"
label="UserName"
helperText="Enter your Username"
value={this.state.username}
onChange={this.handleChange}
required={true}
style={styles}
/>
<br />
<TextField
id="password"
name="password"
type="password"
helperText="Enter your password"
label="Password"
onChange={this.handleChange}
required={true}
style={styles}
/>
<br />
<br />
<br/>
<Button
type="submit"
value="Submit"
variant="contained"
color="primary"
>Submit</Button>
<br />
<br/>
<Alert variant="info">
<Alert.Heading>Don't Have An Account Setup?</Alert.Heading>
<div>
<Link to={`${this.props.match.url}/register`}>Register Here</Link>
</div>
</Alert>
</form>
</div>
)
}
}
解决方案
默认情况下,react-router
属性(例如match
)只会自动注入到由路由渲染的组件中(Home
在这种情况下)。withRouter
您可以使用包装器将它们添加到您的组件中。然后,您必须使用包装组件而不是内部组件。
import {withRouter} from 'react-router';
...
class LoginInternal extends React.Component<MyProps & RouteComponentProps, IState> {
...
export const Login = withRouter(LoginInternal);
推荐阅读
- typescript - 为什么来自 any[] 的元组推理变为 [string, string, ... ] 而从 string[] 变为 ['literal1', 'literal2', ... ]
- firebase-performance - HttpMetric可以直接设置时间而不调用start/stop吗?
- git - Gitlab网站合并请求:git push origin master "一切都是最新的"
- java - 如何在 JUnit 的临时目录中添加文件
- ansible - Ansible“如何遍历列表”以创建新列表
- jquery - jQuery 选择器不适用于 Polymer2 shady dom 模板
- excel - 如何修复运行时错误“7”内存不足,即使在保存、关闭、重新启动计算机后仍然存在
- ruby-on-rails - nil:NilClass 的未定义方法“id”错误
- excel - 如何激活具有部分名称的工作簿?
- android - 如何在 Android 上使用 Achartengine 设置主要和次要网格线?