简体   繁体   中英

Add external Javascript (not jsx) to React Native?

New React Native user here. I'm trying to add the below Transliterate JS Code from a website to React Native.

<script type="text/javascript" src="https://www.google.com/jsapi">
</script>
<script type="text/javascript">

  // Load the Google Transliterate API
  google.load("elements", "1", {
        packages: "transliteration"
      });

  function onLoad() {
    var options = {
        sourceLanguage:
            google.elements.transliteration.LanguageCode.ENGLISH,
        destinationLanguage:
            [google.elements.transliteration.LanguageCode.CHINESE],
        shortcutKey: 'ctrl+g',
        transliterationEnabled: true
    };

    // Create an instance on TransliterationControl with the required
    // options.
    var control =
        new google.elements.transliteration.TransliterationControl(options);

    // Enable transliteration in the textbox with id
    // 'transliterateTextarea'.
    control.makeTransliteratable(['transliterateTextarea']);
  }
  google.setOnLoadCallback(onLoad);
</script>

Below is the HTML code for the script.

<body>
Type in Chinese (Press Ctrl+g to toggle between English and Chinese)<br>
<textarea id="transliterateTextarea" style="width:600px;height:200px"> 
</textarea>
</body>

How can I add use this code in React Native? Been struggling with this for weeks. Thank you!

I would make a Higher Order Component (HOC) that wraps an element with the transliteration. You would use the library that Robbie refers to in the comment to your original question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM