React-native-gesture-handler: CRASH with Swipeable when setting state on componentDidMount

Created on 29 May 2018  路  9Comments  路  Source: software-mansion/react-native-gesture-handler

Argument 1 (NSNumber) of RNGestureHandlerModule.attachGestureHandler must not be null
image

Gesture handler version: 1.0.1

I made a simple reproduction repo of the problem here: https://github.com/henrikra/gesture-handler-crash/blob/master/App.js

Steps to reproduce:

  1. Use Swipeable somewhere in your component
  2. Use setState in componentDidMount
  3. See crash! 馃挜

Workaround: Add setTimeout with 0 delay and it will not crash. But this is very flake workaround and should not be used

I hope this gets fixed soon since this is very critical bug :/

Most helpful comment

Hi there,

I encountered that error using a custom component as a child to PanGestureHandler that was using View but without passing the nativeProps down.

<PanGestureHandler
    {...this.props}
    onGestureEvent={this._onGestureEvent}
    onHandlerStateChange={this._onHandlerStateChange}
    id="dragbox"
>
    <MyComponent>...</MyComponent>
</PanGestureHandler>

Adding :

class MyComponent extends Component {
+    setNativeProps(props: Object) {
+        this.ref.setNativeProps(props);
+    }

+    getRef = (ref: View) => {
+        this.ref = ref;
+    };

    render() {
        return (
            <View
                {...this.props}
+                ref={this.getRef}
                style={[styles.view, this.props.style]}
            >
                ...
            </View>
        );
    }
}

to my component fixed it.

Hopefully it can help other devs looking at that issue to figure out what their problem is 馃檪

All 9 comments

@henrikra
Great thanks for this issue and hope my solution will fix this problem as it will be merged

@henrikra
Should be workable

Please close the issue when the OP has confirmed that the fix works 馃憤

Because the fix fixes only iOS 馃敶 On Android I am getting this error now with the same repo as above

image

So can you reopen the issue? :)

Ping also @kmagiera

Thanks a ton @henrikra for reporting and providing an excellent repro scenario with a full project on Github. That was really helpful. I hope for this issue to be finally resolved. Just published hotfil release 1.0.4. that includes my fix from #202

Feels like it is working now! I report if I find something else 馃帀

Hi there,

I encountered that error using a custom component as a child to PanGestureHandler that was using View but without passing the nativeProps down.

<PanGestureHandler
    {...this.props}
    onGestureEvent={this._onGestureEvent}
    onHandlerStateChange={this._onHandlerStateChange}
    id="dragbox"
>
    <MyComponent>...</MyComponent>
</PanGestureHandler>

Adding :

class MyComponent extends Component {
+    setNativeProps(props: Object) {
+        this.ref.setNativeProps(props);
+    }

+    getRef = (ref: View) => {
+        this.ref = ref;
+    };

    render() {
        return (
            <View
                {...this.props}
+                ref={this.getRef}
                style={[styles.view, this.props.style]}
            >
                ...
            </View>
        );
    }
}

to my component fixed it.

Hopefully it can help other devs looking at that issue to figure out what their problem is 馃檪

I've encountered the same problem when wrapping custom component with NativeViewGestureHandler. When using function components, just use React.forwardRef and pass the ref to native component. I.e:

Parent.jsx:

    <NativeViewGestureHandler ref={listRef} simultaneousHandlers={drawerRef}>
        <ProjectList />
    </NativeViewGestureHandler>

ProjectList.jsx

export const ProjectList = React.forwardRef((props, ref) => {
...

return (
        <FlatList
            ref={ref}
        />
    );
});

Still getting the same error

Was this page helpful?
0 / 5 - 0 ratings