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.

<Bar> onClick handler is called on render, not on click

See original GitHub issue

I’m noticing something odd - it seems that the <Bar> component’s onClick handler is called when it is rendered.

I noticed it while following the example in VX’s gallery: https://vx-demo.now.sh/bars

Here is code to reproduce it.

It has a console.log on render, when the SVG is clicked, and when the Bar is clicked. Notice that the console.logs for the bar being clicked are all present after each render.

import React, { Component } from 'react';
import { Bar } from '@vx/shape';
import { Group } from '@vx/group';
import { GradientTealBlue } from '@vx/gradient';
import { letterFrequency } from '@vx/mock-data';
import { scaleBand, scaleLinear } from '@vx/scale';
import { extent, max } from 'd3-array';

export default class VXBarTest extends Component {
  render(){
    console.log('render');
    const data = letterFrequency.slice(5);

    function round(value, precision) {
      var multiplier = Math.pow(10, precision || 0);
      return Math.round(value * multiplier) / multiplier;
    }

    // accessors
    const x = d => d.letter;
    const y = d => +d.frequency * 100;

    const width = 750;
    const height = 400;

    if (width < 10) return null;

    // bounds
    const xMax = width;
    const yMax = height - 120;

    // scales
    const xScale = scaleBand({
      rangeRound: [0, xMax],
      domain: data.map(x),
      padding: 0.4,
    });

    const yScale = scaleLinear({
      rangeRound: [yMax, 0],
      domain: [0, max(data, y)],
    });

    const bars = data.map((d, i) => {
      const barHeight = yMax - yScale(y(d));

      return (
        <Group key={`bar-${x(d)}`}>
          <Bar
            width={xScale.bandwidth()}
            height={barHeight}
            x={xScale(x(d))}
            y={yMax - barHeight}
            fill="rgba(23, 233, 217, .5)"
            onClick={ function(){ console.log('bar clicked'); }}
          />
        </Group>
      );
    });

    return (
      <svg
        width={width}
        height={height}
        onClick={ function(){ console.log('svg clicked'); }}>
        <GradientTealBlue id="teal" />
        <Group top={40}>
          {bars}
        </Group>
      </svg>
    );
  }
}

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
hshoffcommented, Mar 8, 2018

@jnelken you probably have something like

onClick={function(event) {
  // ...
}}

when you want

onClick={function(data) {
  return function(event) {
    // ...
  };
}}
0reactions
jnelkencommented, Mar 8, 2018

Can we open this back up? It is still not available. It is still called on render for me (“(at)vx/shape”: “^0.0.153”)

Read more comments on GitHub >

github_iconTop Results From Across the Web

<Bar> onClick handler is called on render, not on click #50
I'm noticing something odd - it seems that the <Bar> component's onClick handler is called when it is rendered.
Read more >
Why is my onClick being called on render? - React.js
Notice how with onClick={() => alert('click')}, we're passing a function as the onClick prop. React will only call this function after a click....
Read more >
How to change states with onClick event in ReactJS using ...
Set click event handler function of the element upon which click, results in changing state. In the ComponentOne.js file write the following ...
Read more >
Event Bubbling and Event Catching in JavaScript and React
She has a single parent div with an onClick event handler that, when clicked, calls everyone to the table to eat her food....
Read more >
Render Functions & JSX | Vue.js
Props with names that start with on followed by an uppercase letter are treated as event listeners. For example, onClick is the equivalent...
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