mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/36578 This change brings to the Fabric Interop Layer the possibility to directly call native methods on the View Manager, something that was not possible before and that we can use to simplify the migration to the New Architecture. [iOS][Added] - Native Components can now call native methods in Fabric when they are loaded through the Interop Layer Reviewed By: sammy-SC Differential Revision: D43945278 fbshipit-source-id: fe67ac85a5d0db3747105f56700d1dbba7ada5f1
56 lines
1.4 KiB
JavaScript
56 lines
1.4 KiB
JavaScript
/**
|
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*
|
|
* @flow strict-local
|
|
* @format
|
|
*/
|
|
|
|
import * as React from 'react';
|
|
import type {HostComponent} from 'react-native';
|
|
import type {ViewProps} from 'react-native/Libraries/Components/View/ViewPropTypes';
|
|
import {requireNativeComponent, UIManager} from 'react-native';
|
|
import ReactNative from '../../../react-native/Libraries/Renderer/shims/ReactNative';
|
|
|
|
type ColorChangedEvent = {
|
|
nativeEvent: {
|
|
backgroundColor: {
|
|
hue: number,
|
|
saturation: number,
|
|
brightness: number,
|
|
alpha: number,
|
|
},
|
|
},
|
|
};
|
|
|
|
type NativeProps = $ReadOnly<{|
|
|
...ViewProps,
|
|
opacity?: number,
|
|
color?: string,
|
|
onColorChanged?: (event: ColorChangedEvent) => void,
|
|
|}>;
|
|
|
|
export type MyLegacyViewType = HostComponent<NativeProps>;
|
|
|
|
export function callNativeMethodToChangeBackgroundColor(
|
|
viewRef: React.ElementRef<MyLegacyViewType> | null,
|
|
color: string,
|
|
) {
|
|
if (!viewRef) {
|
|
console.log('viewRef is null');
|
|
return;
|
|
}
|
|
UIManager.dispatchViewManagerCommand(
|
|
ReactNative.findNodeHandle(viewRef),
|
|
UIManager.getViewManagerConfig('RNTMyLegacyNativeView').Commands
|
|
.changeBackgroundColor,
|
|
[color],
|
|
);
|
|
}
|
|
|
|
export default (requireNativeComponent(
|
|
'RNTMyLegacyNativeView',
|
|
): HostComponent<NativeProps>);
|