获取 React Native 中的常量。

huangapple 未分类评论66阅读模式
英文:

getConstants in React native

问题

我正在尝试学习React Native中的原生开发。

我正在阅读React Native文档,上面写着:

一个可选的方法叫做getConstants,它返回暴露给JavaScript的常量值。虽然不需要实现它,但用于将需要从JavaScript同步到Java的预定义键值有效地传递非常有用。

具有以下代码片段:

@Override
public Map<String, Object> getConstants() {
  final Map<String, Object> constants = new HashMap<>();
  constants.put(DURATION_SHORT_KEY, Toast.LENGTH_SHORT);
  constants.put(DURATION_LONG_KEY, Toast.LENGTH_LONG);
  return constants;
}

通过上述定义,我能理解这是将常量信息从JavaScript传递到Java(例如API密钥)的一次性传递。

为了确认,我访问了react-native-maps仓库。

在那里,我看到它们像这样使用:

@Override
public Map<String, Object> getConstants() {
  final Map<String, Object> constants = new HashMap<>();
  constants.put("legalNotice", "This license information is displayed in Settings > Google > Open Source on any device running Google Play services.");
  return constants;
}

并且它没有使用传递的任何内容。

所以,有人可以解释一下上面的定义稍微详细一些吗?getConstants()方法有什么用途?

英文:

I am trying to learn native development in React Native

I was going through React Native docs and it says

> An optional method called getConstants returns the constant values
> exposed to JavaScript. Its implementation is not required but is very
> useful to key pre-defined values that need to be communicated from
> JavaScript to Java in sync

with following snippet

@Override
  public Map&lt;String, Object&gt; getConstants() {
    final Map&lt;String, Object&gt; constants = new HashMap&lt;&gt;();
    constants.put(DURATION_SHORT_KEY, Toast.LENGTH_SHORT);
    constants.put(DURATION_LONG_KEY, Toast.LENGTH_LONG);
    return constants;
  }

From the above defination, I was able to make out that this pass constant one time use information from Javascript to java (such as api key)

To confirm, I went to react-native-maps repo.

There I saw they have used it like this

@Override
  public Map&lt;String, Object&gt; getConstants() {
    final Map&lt;String, Object&gt; constants = new HashMap&lt;&gt;();
    constants.put(&quot;legalNotice&quot;, &quot;This license information is displayed in Settings &gt; Google &gt; Open Source on any device running Google Play services.&quot;);
    return constants;
  }

and it isn't consuming anything passed.

So can someone please explain me the above definition in slightly more details? and what is it use (getConstants() method)?

答案1

得分: 0

getConstants 定义了在 JavaScript 环境中可供使用的常量。在希望让 JavaScript 指定某些参数时非常有用。

来自 React Native 文档的示例:

  @ReactMethod
  public void show(String message, int duration) {
    Toast.makeText(getReactApplicationContext(), message, duration).show();
  }

  @Override
  public Map<String, Object> getConstants() {
    final Map<String, Object> constants = new HashMap<>();
    constants.put("SHORT", Toast.LENGTH_SHORT);
    constants.put("LONG", Toast.LENGTH_LONG);
    return constants;
  }

这将创建一个名为 SHORT 的常量,其值为 Toast.LENGTH_SHORT(整数)。因此,在 JavaScript 端,它们可以像这样使用该常量:

ToastModule.show('message', ToastModule.SHORT);

这样,JavaScript 不需要知道 Toast.LENGTH_SHORT 的实际整数值,而是使用由原生模块(ToastExample.SHORT)暴露的常量。

英文:

getConstants defines constant that can be consumed in JavaScript world. It is useful when you want to let JavaScript specify some parameters.

Example from react native doc:

  @ReactMethod
  public void show(String message, int duration) {
    Toast.makeText(getReactApplicationContext(), message, duration).show();
  }

  @Override
  public Map&lt;String, Object&gt; getConstants() {
    final Map&lt;String, Object&gt; constants = new HashMap&lt;&gt;();
    constants.put(&quot;SHORT&quot;, Toast.LENGTH_SHORT);
    constants.put(&quot;LONG&quot;, Toast.LENGTH_LONG);
    return constants;
  }

This will create a constant SHORT with value of Toast.LENGTH_SHORT (integer). so on JavaScript side they can use the constant like

ToastModule.show(&#39;message&#39;, ToastModule.SHORT);

This way JavaScript doesn't have to know actual integer value of Toast.LENGTH_SHORT and instead use the constant exposed by native module (ToastExample.SHORT).

huangapple
  • 本文由 发表于 2020年5月31日 06:58:40
  • 转载请务必保留本文链接:https://java.coder-hub.com/62109629.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定