1

I am newbie in react. I am working on routing. I found that when I click on link, the URL change as per the link clicked, but it doesn't navigate to the next screen: https://jsfiddle.net/v78kncLb/

import {
    BrowserRouter as Router,
    Route,
    Link,
} from 'react-router-dom'
import Demo from "./demo";

<Router>
                <div className='input-form'>
                    <form>
                        <label>Name</label><br/>
                        <input type="text" name="username" onChange={this.onChange}
                               value={this.state.username}/><br/><br/>

                        <label>Password</label>
                        <input type="text" name="password" onChange={this.onChange}
                               value={this.state.password}/><br/><br/>

                        <label>Confirm Password</label>
                        <input type="text" name="confirm" onChange={this.onChange}
                               value={this.state.confirm}/><br/><br/>

                        <label>E-mail</label><br/>
                        <input type="text" name="email" onChange={this.onChange} value={this.state.email}/><br/><br/>

                        <label>Aadhar</label><br/>
                        <input type="text" name="aadhar" onChange={this.onChange} value={this.state.aadhar}/><br/><br/>

                        <label>Pan</label><br/>
                        <input type="text" name="pan" onChange={this.onChange} value={this.state.pan}/><br/><br/>

                        <label>Voter</label><br/>

                        <Link to='./demo' className="btn btn-primary">hello</Link>
                        <Route exact path="/"/>
                        <Route path="./demo" component={Demo}/>
                    </form>
                </div>
            </Router>
2
  • You can learn from working example: codesandbox.io/s/6p2p1zzn and codesandbox.io/s/vVoQVk78 Commented Sep 13, 2019 at 13:27
  • Have tried making sample its working absolutely fine but when i implemented in this its not working @Oleg Commented Sep 13, 2019 at 13:29

3 Answers 3

1

Problem is with you path. Update it by like below ( no dot before path)

<Link to='./demo' className="btn btn-primary">hello</Link>
<Route exact path="/"/>
<Route path="/demo" component={Demo}/>
Sign up to request clarification or add additional context in comments.

Comments

1

You need to put switch after Router like ;

import { BrowserRouter as Router, Route, Switch } from 'react-router-dom'

const App = () => {
  return(
    <Router>
      <div className='app'>
        <Switch>
          <Route exact path="/"/>
          <Route path="/demo" component={Demo}/>
        </Switch>
      </div>
    </Router>
  )
}

1 Comment

Also you need to delete . before /demo
0

Remove the dot '.' from the Route path and Link. Just use this <Route path="/demo" component={Demo}/>

2 Comments

thanks ..it worked for me but text is appending in the ./App file..what should i do for that?? Thanks a lot..@Md Isfar Uddin AINur
Sorry did not understand. Could you explain it a little bit more?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.