Reputation: 1114
As I am new to react and typescript this is probably a simple mistake, but I have checked other answers I found about this and nothing has worked. I would appreciate any help and an explanation of what I am doing wrong so I can learn. I cannot seem to get react-router to work with typescript at all. It keeps throwing an error that it "can't resolve react-router-dom" in the following code. I attempted to follow a basic tutorial for react-router and typescript I found here: https://riptutorial.com/react-router/example/30226/basic-routing
So basically what I want to happen is for the login screen to come up with Username and Password fields to pop up and a submit button underneath it. Then underneath that I have a link that they can click on to register if they don't already have an account, which will take them to another page.
I will put the Register class in its own file and import it once this works, I just didn't want to have to show two different files for the example:
import React from 'react';
import ReactDOM from 'react-dom';
import { BrowserRouter as Router, Route, Link } from "react-router-dom";
import TextField from '@material-ui/core/TextField';
import Button from '@material-ui/core/Button';
import { Alert } from 'react-bootstrap';
interface IState {
[key: string]: any; // or the type of your input
}
const styles = {
background: 'lightblue'
};
export class Login extends React.Component<{}, 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) {
//TODO: we are going to 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="/register">Register Here</Link>
</div>
</Alert>
</form>
</div>
)
}
}
class Register extends React.Component<{}, {}> {
render() {
return (
<div>
<h1>Register</h1>
</div>
)
}
}
ReactDOM.render(
<Router>
<div>
<Route path="/register" component={Register} />
</div>
</Router>
, document.getElementById('root'));
Upvotes: 2
Views: 3915
Reputation: 1366
You just have to install the @types (@types/react-router-dom) that provide the types definition and install react-router-dom:
npm install -S react-router-dom @types/react-router-dom
or
yarn add react-router-dom @types/react-router-dom
Upvotes: 3
Reputation: 367
You can use Redirect of react-router-dom to use redirect like below
import { Redirect} from 'react-router-dom'
render() {
if(condition===true){
return (
<Redirect to='/your-route-path' />
)
}
}
Upvotes: 0