Using the example native-modules in the documentation is an error #11447
-
When I use the example from https://microsoft.github.io/react-native-windows/docs/native-modules I get the error Problem reproduction
Create a new project npx react-native init AskIt
cd AskIt
npx react-native-windows-init --overwrite Add these codes in React Native import type {TurboModule} from 'react-native/Libraries/TurboModule/RCTExport';
import {TurboModuleRegistry} from 'react-native';
export interface Spec extends TurboModule {
getConstants: () => {
E: number;
PI: number;
};
add(a: number, b: number): Promise<number>;
}
export default TurboModuleRegistry.get<Spec>('FancyMath') as Spec | null;
// /**
// * @format
// */
//
// import {AppRegistry} from 'react-native';
// import App from './App';
import {name as appName} from './app.json';
//
// AppRegistry.registerComponent(appName, () => App);
import React, {Component} from 'react';
import {AppRegistry, Alert, Text, View, Button} from 'react-native';
import FancyMath from './src/lib/NativeFancyMath';
import {NativeEventEmitter} from 'react-native';
const FancyMathEventEmitter = new NativeEventEmitter(FancyMath);
class App extends Component {
eventHandler(result) {
console.log('Event was fired with: ' + result);
}
componentDidMount() {
// Subscribing to FancyMath.AddEvent
FancyMathEventEmitter.addListener('AddEvent', this.eventHandler, this);
}
componentWillUnmount() {
// Unsubscribing from FancyMath.AddEvent
FancyMathEventEmitter.removeListener('AddEvent', this.eventHandler, this);
}
_onPressHandler() {
// Calling FancyMath.add method
FancyMath.add(
/* arg a */ FancyMath.Pi,
/* arg b */ FancyMath.E,
/* callback */ function (result) {
Alert.alert(
'FancyMath',
`FancyMath says ${FancyMath?.Pi} + ${FancyMath?.E} = ${result}`,
[{text: 'OK'}],
{cancelable: false},
);
},
);
}
render() {
return (
<View>
<Text>FancyMath says PI = {FancyMath?.Pi}</Text>
<Text>FancyMath says E = {FancyMath?.E}</Text>
<Button onPress={this._onPressHandler} title="Click me!"/>
</View>
);
}
}
AppRegistry.registerComponent(appName, () => App);
"codegenConfig": {
"name": "AskIt",
"type": "modules",
"jsSrcsDir": "src/lib",
"windows": {
"namespace": "AskIt"
}
} Add these codes in windows solution // Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
#pragma once
#include "pch.h"
#include <functional>
#define _USE_MATH_DEFINES
#include <math.h>
#include "NativeModules.h"
#include "../../codegen/NativeFancyMathSpec.g.h"
namespace App
{
REACT_MODULE(FancyMath); // 模块名称
struct FancyMath
{
using ModuleSpec = FancyMathSpec;
REACT_GET_CONSTANTS(GetConstants) // 返回的常量
FancyMathSpec_Constants GetConstants() noexcept
{
FancyMathSpec_Constants constants;
constants.E = M_E;
constants.Pi = M_PI;
return constants;
}
REACT_METHOD(Add, L"add");
double Add(double a, double b) noexcept
{
double result = a + b;
AddEvent(result);
return result;
}
REACT_EVENT(AddEvent);
std::function<void(double)> AddEvent;
};
}
#pragma once
#include "winrt/Microsoft.ReactNative.h"
namespace winrt::AskIt::implementation
{
struct ReactPackageProvider : winrt::implements<ReactPackageProvider, winrt::Microsoft::ReactNative::IReactPackageProvider>
{
public: // IReactPackageProvider
void CreatePackage(winrt::Microsoft::ReactNative::IReactPackageBuilder const &packageBuilder) noexcept;
};
} // namespace winrt::AskIt::implementation
namespace winrt::AskIt::factory_implementation
{
struct ReactPackageProvider : ReactPackageProviderT<ReactPackageProvider, implementation::ReactPackageProvider> {};
} // namespace winrt::AskIt::factory_implementation
#include "pch.h"
#include "ReactPackageProvider.h"
#include "NativeModules.h"
// NOTE: You must include the headers of your native modules here in
// order for the AddAttributedModules call below to find them.
#include "FancyMath.h"
using namespace winrt::Microsoft::ReactNative;
namespace winrt::AskIt::implementation
{
void ReactPackageProvider::CreatePackage(IReactPackageBuilder const &packageBuilder) noexcept
{
AddAttributedModules(packageBuilder, true);
}
} // namespace winrt::AskIt::implementation Run At this point the program throws an error
I noticed that what should i do, thanks |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
This codegen is very new and may not be ready until 0.72. We're currently debating whether the right things to unblock this landed in 0.71. |
Beta Was this translation helpful? Give feedback.
Moved over to this issue: #11449
Can you help provide the requested information over there? I @ mentioned you.