4

I'm recently using react-spring and it works really well but I'm having some issues when I have to apply an onClick on childs.

Here is my main structure:

<anim.button  className="c"
   style={{ opacity: opacity.interpolate(o => 1 - o), transform }}>
   <Bar />
</anim.button>
<anim.button  className="c"
   style={{ opacity, transform: transform.interpolate(t => `${t} 
   rotateX(180deg)`) }}>
   <Bar />
</anim.button>

An onClick on the anim.button would work perfectly fine of course, but that's not what I'm trying.

Here is my <Bar /> component:

  const Bar = () => (
    <div className="bar">
      <Checkbox
        onClick={() => {
          set(state => !state)
        }}
      />
    </div>
  )

I also tried using the <anim.div> instead of an <anim.button> but this gives me the same results.

Now the problem here is that it seems like react-spring puts a layer onto my <Bar /> component. Is there a way to use the onClick in my component instead of the one that is used onto anim.button?

Trisma
  • 735
  • 6
  • 19

4 Answers4

1

Given that Checkbox is also a component and it might not have onClick handled try putting onClick on the wrapping div or inspect the Checkbox component.

Jayakumar Thangavel
  • 1,884
  • 1
  • 22
  • 29
1

Haven't used react-spring ever but structuring your Bar component like this may do the trick.

const Bar = (props) => (
<div onClick={() => {set(state => !state)}}>
 <anim.button  {...props}>
   <div className="bar">
     <Checkbox />
   </div>
 </anim.button>
</div>)

then rendering it like this:

<Bar className={'c'} style={{ opacity: opacity.interpolate(o => 1 - o), transform}}/>
<Bar className="c" style={{ opacity, transform: transform.interpolate(t => `${t}
    rotateX(180deg)`) }}/>
Japsz
  • 785
  • 6
  • 14
  • But this will result in the same problem. The while bar is clickable when it shouldn't. Only the checkbox should – Trisma Nov 15 '19 at 08:33
0

You could rather wrap the Bar in a div: <animated.div> you are wrapping a button around a checkbox which is not great. onClick will trigger as expected.

<animated.div className="c"
   style={{ opacity, transform: transform.interpolate(t => `${t} 
   rotateX(180deg)`) }}>
   <Bar />
</animated.div>
Samuel Goldenbaum
  • 18,391
  • 17
  • 66
  • 104
  • This is what I initially did but this don't work either. only the `onClick` on `` is used. The div becomes clickable but not the children – Trisma Nov 15 '19 at 08:27
0

Issue has been found and didn't actually has anything to do with react-spring. The position absolute styling was causing this.

Trisma
  • 735
  • 6
  • 19