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.

ToggleButtonGroup of onChange doesn't work when ToggleButton has a wrapper around it

See original GitHub issue

Today we were trying to create several custom toggle buttons inside a ToggleButtonGroup as we need different styles depending on certain fields.

The following wouldn’t work; <ToggleButtonGroup onChange={handleChange}> <CustomToggleButton ... /> CustomToggleButton is literally just a standard ToggleButton but it applies different classNames.

If we changed CustomToggleButton to be ToggleButton then everything works as normal. Is is possible to achieve this? I hope I’m just being dumb and missing something obvious here.

We passed every prop down to the button and even used forwardRef just as a means to see if it would work but it didn’t.

We have many different styles to apply so it would be ideal to have a CustomToggleButton to handle these. Worst case scenario we’ll create a function to dictate which style to apply, but it would be preferable to use the above approach.

Thanks all.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:4
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

6reactions
eps1loncommented, May 14, 2020

Current workaround:

function CustomToggleButton(props) {
  return <ToggleButton {...props} />
}

And be sure to merge all own props properly.

1reaction
eps1loncommented, May 15, 2020

Let’s keep this open and leave the umbrella issue separate. I have a working solution with context that was blocked by missing cleanup.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Can't use onChange with react-bootstrap ToggleButtonGroup
The main issue is I can't manage to make the onChange event work on a ToggleButtonGroup. My code: class ToggleButtonGroupControlled extends ...
Read more >
I'm building a simple ToggleButtonGroup. Which component ...
Where <MyToggleButton> is a thin wrapper around the underlying HTML checkbox/button. Each item has its own set of props and callback handlers. I ......
Read more >
Toggle button - Luna
Use toggle buttons to let users quickly switch between views in the same context, for example changing a grid or list view on...
Read more >
ToggleButton | Amplify UI for React
A toggle button can be a controlled component when given isPressed prop and onChange prop must be provided in this case. Press me!...
Read more >
React MUI ToggleButtonGroup API - GeeksforGeeks
Step 4: Run the project as follows: npm start. Example 1: In the following example, we have single select options in the toggle...
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