<Bar> onClick handler is called on render, not on click
See original GitHub issueI’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:
- Created 6 years ago
- Comments:7 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@jnelken you probably have something like
when you want
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”)