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.

question : how to call js callback like setState after runTiming()

See original GitHub issue

my use-case is to call a setState after runTiming or runSpring was done springing the value. here is my code


function runSpring({ translateY, velocityY, state: _state }) {
  const clock = new Clock();
  const state = {
    finished: new Value(0),
    velocity: new Value(0),
    position: new Value(0),
    time: new Value(0)
  };
  const config = {
    stiffness: new Value(10),
    mass: new Value(1),
    damping: new Value(10),
    overshootClamping: false,
    restSpeedThreshold: 0.001,
    restDisplacementThreshold: 0.001
  };
  const configB = SpringUtils.makeConfigFromBouncinessAndSpeed({
    ...SpringUtils.makeDefaultConfig(),
    bounciness: 10,
    speed: 2
  });
  const configC = SpringUtils.makeConfigFromOrigamiTensionAndFriction({
    ...SpringUtils.makeDefaultConfig(),
    tension: 100,
    friction: new Value(40)
  });
  // startClock(clock);
  return block([
    cond(lessOrEq(translateY, 0), [
      cond(
        lessOrEq(velocityY, -300),
        [
          startClock(clock),
          spring(clock, state, { ...config, toValue: -DeviceHeight })
        ],
        [
          cond(
            eq(_state, State.END),
            [startClock(clock), spring(clock, state, config)],

            [
              set(state.position, translateY)
              // debug("clock  2 happened", eq(_state, State.ACTIVE))
            ]
          )
        ]
      )
    ]),
    state.position
  ]);
}

port default class ExampleTab extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      currentItemIndex: 0

    };
    this._translateY = new Value(0);
    this._state = new Value(State.UNDETERMINED);
    this._velocityY = new Value(0);
    this.currentCardVelocity = new Value(0);
    this.translateY = runSpring({
      translateY: this._translateY,
      velocityY: this._velocityY,
      state: this._state
    });
  }

  render(){
 return (
        <PanGestureHandler
              key={pIndex}
              {...gestureEvent}
              // onHandlerStateChange={this.handleGState}
              enabled={indexDistance === 0}
            >
              <Animated.View
                style={{
                  flex: 1,
                  shadowOffset: { height: 3 },
                  elevation: 3,
                  // borderWidth: 0.5,
                  borderColor: "black",
                  borderRadius: 25,
                  shadowColor: "black",
                  shadowOpacity: 0.5,
                  justifyContent: "flex-end",
                  transform: [
                    {
                      translateY:
                        indexDistance === 0
                          ? this.translateY
                          : isPrevCard
                          ? prevCardTranslateY
                          : translateY
                    }
                  ],
                  // opacity: animatedOpacity,
                  marginHorizontal: multiply(indexDistance, secondCardGrowth),
                  ...StyleSheet.absoluteFillObject
                }}
              >
              </Animed.View>
           </PanGestureHandler>
        ) }
  }
}

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:15 (6 by maintainers)

github_iconTop GitHub Comments

5reactions
brunocrpontescommented, Oct 16, 2019

You can do it like below.

Using conditionals with the finished state at the end of the block and the call function to do a callback from js side:

[ ...previous block animated events...,
  cond(state.finished, 
     call([...your params here], (...your params) => {...function })
  )
]

2reactions
sidferreiracommented, Dec 26, 2019

So I’m not the only one…

Read more comments on GitHub >

github_iconTop Results From Across the Web

React hooks: accessing up-to-date state from within a callback
I need to call setState to access the previous state. Even though I have no intention of setting the state. I feel like...
Read more >
React: useState hook with callback - Maksim Ryzhikov - Medium
In the old “class” oriented React version you could call `setState` and pass as a second argument function which would be called when...
Read more >
What is the purpose of callback function as an argument of ...
There can be a requirement to perform some action only after the state has updated. We can do this in React. SetState is...
Read more >
What is the purpose of _callback function_ as an argument of ...
Get Answer to What is the purpose of _callback function_ as an argument of `setState`? And Kill Your Next Tech Interview.
Read more >
What is the purpose of a callback function as an argument of ...
setState() is an asynchronous function , that means it is running simultaneously with program ... So I say, "Please answer this question when...
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