Coder Social home page Coder Social logo

on Android,After upgrading React Native from 0.64.2 to 0.72.5, constants returned via getContents in bridge classes cannot be used in JavaScript about react-native HOT 9 CLOSED

zwyzzu avatar zwyzzu commented on April 28, 2024
on Android,After upgrading React Native from 0.64.2 to 0.72.5, constants returned via getContents in bridge classes cannot be used in JavaScript

from react-native.

Comments (9)

github-actions avatar github-actions commented on April 28, 2024
⚠️ Missing Reproducible Example
ℹ️ We could not detect a reproducible example in your issue report. Please provide either:
  • If your bug is UI related: a Snack
  • If your bug is build/update related: use our Reproducer Template. A reproducer needs to be in a GitHub repository under your username.

from react-native.

github-actions avatar github-actions commented on April 28, 2024
⚠️ Newer Version of React Native is Available!
ℹ️ You are on a supported minor version, but it looks like there's a newer patch available - 0.72.12. Please upgrade to the highest patch for your minor or latest and verify if the issue persists (alternatively, create a new project and repro the issue in it). If it does not repro, please let us know so we can close out this issue. This helps us ensure we are looking at issues that still exist in the most recent releases.

from react-native.

cortinico avatar cortinico commented on April 28, 2024

What is the question @zwyzzu ?

from react-native.

zwyzzu avatar zwyzzu commented on April 28, 2024

In the JAVA class (RTNCalculatorTurbo extends NativeRTNCalculatorSpec), the getContents method is overridden to return a map (with map.put("valueStr", "valueString")). In 0.64.2, we were able to reference the constant (RTNCalculator.valueStr) normally, but after upgrading to 0.72.5, using RTNCalculator.valueStr shows undefined

from react-native.

cortinico avatar cortinico commented on April 28, 2024

In the JAVA class (RTNCalculatorTurbo extends NativeRTNCalculatorSpec), the getContents method is overridden to return a map (with map.put("valueStr", "valueString")). In 0.64.2, we were able to reference the constant (RTNCalculator.valueStr) normally, but after upgrading to 0.72.5, using RTNCalculator.valueStr shows undefined

How does your JS file look like for Calculator?
Can you please provide a valid reproducer?

from react-native.

zwyzzu avatar zwyzzu commented on April 28, 2024

Java

package com.awesomeproject.turbomodule;

import android.util.Log;

import com.autohome.rnmodule.rtncalculator.NativeRTNCalculatorSpec;
import com.awesomeproject.react.RTNCalculatorModuleImpl;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactMethod;

import java.util.HashMap;
import java.util.Map;

public class RTNCalculatorTurbo extends NativeRTNCalculatorSpec {

private static final String TAG = "yedr.RTNCalculatorTurbo";

private ReactApplicationContext reactContext;

private RTNCalculatorModuleImpl impl;

public RTNCalculatorTurbo(ReactApplicationContext reactContext) {
super(reactContext);
this.reactContext = reactContext;
impl = new RTNCalculatorModuleImpl(reactContext);
}

@ReactMethod
public void add(double a, double b, Promise promise) {
// call Delegate
Log.d(TAG, "[newArch]add");
impl.add(a, b, promise);
}

@OverRide
public boolean hasConstants() {
return true;
}

@OverRide
@ReactMethod(isBlockingSynchronousMethod = true)
public Map<String, Object> getConstants() {
final Map<String, Object> map = new HashMap<>();
map.put("valueStr", "valueString");
return map;
}

@OverRide
public void onCatalystInstanceDestroy() {
impl.onCatalystInstanceDestroy();
}

@OverRide
public void initialize() {
impl.initialize();
}

// @OverRide
// public void invalidate() {
// super.invalidate();
// impl.invalidate();
// }
}

JavaScript
import type {TurboModule} from 'react-native/Libraries/TurboModule/RCTExport';
import {TurboModuleRegistry} from 'react-native';

export interface Spec extends TurboModule {
add(a: number, b: number): Promise;
}

export default TurboModuleRegistry.get(
'RTNCalculator',
) as Spec | null;

Using in JavaScript
import RTNCalculator from 'RTNCalculator/js/NativeRTNCalculator.ts'; // TruboModule
console.log('============' + RTNCalculator.valueStr)

from react-native.

zwyzzu avatar zwyzzu commented on April 28, 2024

By logging, it was found that the getContents method of RTNCalculatorTurbo was not called.

from react-native.

zwyzzu avatar zwyzzu commented on April 28, 2024

@cortinico help me

from react-native.

cortinico avatar cortinico commented on April 28, 2024
import type {TurboModule} from 'react-native/Libraries/TurboModule/RCTExport'; import {TurboModuleRegistry} from 'react-native';

+export interface SomeConstants {
+   x: string;
+   y: number;
+   z: boolean;
+}

export interface Spec extends TurboModule { 
+   getConstants(): SomeConstants
	add(a: number, b: number): Promise; 
}

export default TurboModuleRegistry.get( 'RTNCalculator', ) as Spec | null;

In order to support Constants with TurboModules, you will have to follow this syntax

from react-native.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.