react-native-windows icon indicating copy to clipboard operation
react-native-windows copied to clipboard

How to correctly add a `ReactProp` to `ReactScrollViewManager`

Open octaviantu opened this issue 8 years ago • 3 comments

Environment

  1. react-native -v: 0.50.3
  2. npm ls rnpm-plugin-windows:
  3. npm ls react-native-windows: 0.50.0-rc.2
  4. node -v: v8.9.1
  5. npm -v: 5.5.1
  6. yarn --version:

Then, specify:

  • Target Platform: WPF
  • Target Platform Version(s): .NET 4.6.1
  • Target Device(s): Desktop
  • Development Operating System: Windows 10.0.16299
  • Visual Studio Version: Visual Studio 2017

Steps to Reproduce

In my React Native code, I have a FlatList. When I scroll vertically(PreviewMouseWheel) over it nothing happens, even though scrolling vertically works above the rest of the application. Therefore, I added this code to ReactNative.Net46/Scroll/ReactScrollViewManager.cs:

        protected override void AddEventEmitters(ThemedReactContext reactContext, ScrollView view)
        {
           //left the exiting code
           view.PreviewMouseWheel += PreviewMouseWheel;
        }

        private static void PreviewMouseWheel(object sender, MouseWheelEventArgs e)
        {
            if (!e.Handled)
            {
                e.Handled = true;
                var eventArg = new MouseWheelEventArgs(e.MouseDevice, e.Timestamp, e.Delta);
                eventArg.RoutedEvent = UIElement.MouseWheelEvent;
                eventArg.Source = sender;
                var parent = ((Control)sender).Parent as UIElement;
                parent.RaiseEvent(eventArg);
                UIElement grandParent = VisualTreeHelper.GetParent(parent) as UIElement;
                grandParent.RaiseEvent(eventArg);          
            }
        }

This totally solves my problem. It works like a charm.

However, I want to solve my problem more elegantly, by adding a ReactProp:

        [ReactProp("bubbleMouseWheel", DefaultBoolean = false)]
        public void BubbleMouseWheel(ScrollView view, bool enabled)
        {
            if(enabled)
            {
                view.PreviewMouseWheel += PreviewMouseWheel;
            }
        }

        private static void PreviewMouseWheel(object sender, MouseWheelEventArgs e)
        {
            if (!e.Handled)
            {
                e.Handled = true;
                var eventArg = new MouseWheelEventArgs(e.MouseDevice, e.Timestamp, e.Delta);
                eventArg.RoutedEvent = UIElement.MouseWheelEvent;
                eventArg.Source = sender;
                var parent = ((Control)sender).Parent as UIElement;
                parent.RaiseEvent(eventArg);
                UIElement grandParent = VisualTreeHelper.GetParent(parent) as UIElement;
                grandParent.RaiseEvent(eventArg);          
            }
        }

However, my app fails with this message:

error

So did I miss any steps for creating a ReactProp? How else can I create one?

(Write your steps here:)

1.Add this code to ReactNative.Net46/Scroll/ReactScrollViewManager.cs:

        [ReactProp("bubbleMouseWheel", DefaultBoolean = false)]
        public void BubbleMouseWheel(ScrollView view, bool enabled)
        {
            if(enabled)
            {
                view.PreviewMouseWheel += PreviewMouseWheel;
            }
        }

        private static void PreviewMouseWheel(object sender, MouseWheelEventArgs e)
        {
            if (!e.Handled)
            {
                e.Handled = true;
                var eventArg = new MouseWheelEventArgs(e.MouseDevice, e.Timestamp, e.Delta);
                eventArg.RoutedEvent = UIElement.MouseWheelEvent;
                eventArg.Source = sender;
                var parent = ((Control)sender).Parent as UIElement;
                parent.RaiseEvent(eventArg);
                UIElement grandParent = VisualTreeHelper.GetParent(parent) as UIElement;
                grandParent.RaiseEvent(eventArg);          
            }
        }

Expected Behavior

  1. No error.
  2. Scrolling works.

(Write what you thought would happen.)

Actual Behavior

error

Reproducible Demo

(Paste the link to an example project and exact instructions to reproduce the issue.)

I changed NOTHING on the JavaScript side, so I don't need to post a Reproducible Demo.

I only tried to add a ReactProp in the C# code.

So how can I create a ReactProp correctly? Did I miss any steps?

octaviantu avatar Feb 15 '18 19:02 octaviantu

Or is there some alternative and easier way of doing this? Like setting a flag?

octaviantu avatar Feb 16 '18 09:02 octaviantu

You need the javascript to also be aware of this prop. See here for how native components are registered: https://github.com/Microsoft/react-native-windows/blob/master/docs/NativeComponentsWindows.md#5-implement-the-javascript-module

You're trying to add a property to an existing component after it is already registered and that's probably not supported.

That said, it does appear to work if you require the component and then modify it's propTypes

const PropTypes = require('prop-types');
ScrollView.propTypes.bubbleMouseWheel = PropTypes.bool```

martinbooth avatar Feb 23 '18 17:02 martinbooth

@martinbooth's suggestion looks accurate. Also, I believe the common pattern for the native prop that subscribes / unsubscribes to a native event is to use the event callback function name as the native boolean prop (i.e., onMouseWheel or something similar, rather than bubbleMouseWheel).

rozele avatar Jul 23 '18 22:07 rozele