How to correctly add a `ReactProp` to `ReactScrollViewManager`
Environment
react-native -v: 0.50.3npm ls rnpm-plugin-windows:npm ls react-native-windows: 0.50.0-rc.2node -v: v8.9.1npm -v: 5.5.1yarn --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:
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
- No error.
- Scrolling works.
(Write what you thought would happen.)
Actual Behavior
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?
Or is there some alternative and easier way of doing this? Like setting a flag?
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'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).