Event handling in ReactVR

Viewed 203

Been experimenting with ReactVR and i have few doubts in handling events.As per the documentation in Cusors and Input,finding out the type of event is as easy as reading type of the event.

Events coming into onInput can be further filtered by inspecting the type field, which will be one of 'MouseInputEvent', 'KeyboardInputEvent', 'TouchInputEvent', or 'GamepadInputEvent'

But in the below code event.type gives undefined.

_onInput(event) {
    console.log(event.type);//undefined
}

i was eventually able to sniff the event type by doing

_onInput(event) {
    let syntheticEvent = event.nativeEvent.inputEvent;
    console.log(syntheticEvent.type);
}

Is this the expected behaviour or am i missing something.

Few related questions

  1. Do we have to sniff events with event.nativeEvent?
  2. How do we find out the target of the event, for eg if i am to bind an onInput event to a Plane , currently inspecting the event.nativeEvent gives the target as a numerical value? Not helpful at all.
  3. Finally, the ReactJS way of calling events doesn't seem to work in ReactVR. For eg, in the former we would just

    <Plane onInput={this._onInput} lit={true} />
    

    But this doesnt work , in the latter we have to

    <Plane onInput={(event) => this._onInput(event)} lit={true} />
    

    Why is this?

Note:i have some exp with ReactJS and ZERO with React Native, i am guessing ReactVR inclines towards the latter pardon if it is obvious

1 Answers

This happens because the event object in react is reused and gets flushed immediately after it goes out of the function scope therefore you have to store it as a variable for further use.

Related