question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Log React Router <Link/> clicks as actions?

See original GitHub issue

I’m looking for a way to log clicks on <Link/> components (from react-router@2.x.x) as actions. Currently a click throws this error: “Uncaught Invariant Violation: <Link>s rendered outside of a router context cannot navigate.”

Is there a way to do this already? I couldn’t find anything

The links addon is interesting, but a completely unrelated usecase.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:14 (7 by maintainers)

github_iconTop GitHub Comments

5reactions
nathancahillcommented, Apr 14, 2017

Here’s the “correct” way to replace React-Router’s history functions with actions. This is in my config.js:

import { addDecorator, action } from '@kadira/storybook'
import { Router } from 'react-router'
import createMemoryHistory from 'history/createMemoryHistory'

const history = createMemoryHistory()

history.push = action('history.push')
history.replace = action('history.replace')
history.go = action('history.go')
history.goBack = action('history.goBack')
history.goForward = action('history.goForward')

addDecorator(story => <Router history={history}>{story()}</Router>)
5reactions
arunodacommented, Sep 25, 2016

I tried this, here’s how to do this:

First create a file called rr.js inside your Storybook config directory (.storybook) with the following content:

import React from 'react';
import { action } from '@kadira/storybook';

// Export the original react-router
module.exports = require('react-router-original');

// Set the custom link component.
module.exports.Link = class Link extends React.Component {
  handleClick(e) {
    e.preventDefault();
    const { to } = this.props;
    action('Link')(to);
  }

  render() {
    const { children, style } = this.props;

    return (
      <a
        style={style}
        href='#'
        onClick={(e) => this.handleClick(e)}
      >
        {children}
      </a>
    );
  }
};

Then create another file called webpack.config.js and add following content:

// load the default config generator.
var genDefaultConfig = require('@kadira/storybook/dist/server/config/defaults/webpack.config.js');

module.exports = function(config, env) {
  // You can use your own config here as well, instead our default config.
  var config = genDefaultConfig(config, env);

  // this is used by our custome `rr.js` module
  config.resolve.alias['react-router-original'] = require.resolve('react-router');
  // this `rr.js` will replace the Link with a our own mock component.
  config.resolve.alias['react-router'] = require.resolve('./rr.js');
  return config;
};
Read more comments on GitHub >

github_iconTop Results From Across the Web

Log React Router <Link/> clicks as actions? #485 - GitHub
I'm looking for a way to log clicks on <Link/> components (from react-router@2.x.x) as actions. Currently a click throws this error: "Uncaught ...
Read more >
How to dispatch an action when clicking on Link when using ...
Eg: I click on Edit button -> Action is dispatched -> Store updated, {'state': 'edit-mode'} -> Proceed to redirect. Or do you have...
Read more >
How to handle navigation in your app with React Router Link
In this article, we'll explore the navigation possibilities in a React app and we'll take a look at the React Router Link component....
Read more >
Link v6.6.1 - React Router
A <Link> is an element that lets the user navigate to another page by clicking or tapping on it. In react-router-dom , a...
Read more >
Navigation in React App using React Router (v6)
import { Routes, Route, NavLink as Link } from "react-router-dom" ... on certain user action, say on click of a button, react router...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found