K
K
KirylLapouski2018-05-08 13:30:55
JavaScript
KirylLapouski, 2018-05-08 13:30:55

How to follow a link inside an event handler in react?

So far, only this code works: But the page reloads with this approach. How can I follow a link without reloading, and preferably using react-router?
document.location.href = "/cources";

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Spirin, 2018-05-08
@KirylLapouski

import React, { Component } from 'react';
import { withRouter } from 'react-router-dom';

class Example extends Component {
  handler = () => {
    this.props.history.push('/cources');
  };
  
  render() { /* ... */ }
}

export default withRouter(Example);

You may want to use a simple Link component :
import React, { Component } from 'react';
import { Link } from 'react-router-dom';

class Example extends Component {
  render() {
    return <Link to="/cources" />;
  }
}

export default Example;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question