javascriptreactjssdkkaios

How to insert a 3rd party library in ReactJS app


It works perfectly in pure JS, but now I'm trying to import the library for KaiOS SDK avert in my ReactJS app, and I can't, the compilation always fail with this message :

  Line 230:9:  'getKaiAd' is not defined  no-undef

how to do this !? (js library file)

Here is my simplified code, we can see the function call at line 12 fail..

import React, { Component } from 'react'

import './App.css'
import Result from './Result'
import BoutonListe from './BoutonListe'
import Tableau from './Tableau'

class App extends Component {


    addAdvert(element){
        getKaiAd({
            publisher: 'MON ID',
            app: 'ExCurrency',
            test:1, //1 for test, 0 for prod
            onerror: err => console.error('Custom catch:', err),
            onready: ad => {
                // Ad is ready to be displayed
                // custom event
                let button = document.getElementById(element)
                button.addEventListener('focus', function btnListener() {
                    button.removeEventListener('focus', btnListener)
                    // calling 'display' will display the ad
                    ad.call('display');
                })
            }
        });
    }


    componentDidMount(){
        this.addAdvert('ListeG');
    }

    render() {
      const {montant,
             montantRes,
             deviseListeG,
             deviseListeD,
             actualRate,
             dateUpdate,
             styleTab,
             tableauZero,
             nomDevises }= this.state

      return (
        <div>
            <Tableau styli={styleTab}
                     tableauO={tableauZero}
                     noms={nomDevises}
                     onKeyDown={this.handleKeydown}/>
        </div>
      )
  }
}
export default App;

I just added : import {getKaiAd} from './Kaiads.v4.min.js'

and I got :

Failed to compile.

./src/Kaiads.v4.min.js
  Line 1:695:   Expected an assignment or function call and instead saw an expression  no-unused-expressions
  Line 1:792:   Expected an assignment or function call and instead saw an expression  no-unused-expressions
  Line 1:901:   Expected an assignment or function call and instead saw an expression  no-unused-expressions
  Line 1:1327:  Expected an assignment or function call and instead saw an expression  no-unused-expressions
  Line 1:1385:  'getKaiAd' is not defined                                              no-undef
  Line 1:1395:  'getKaiAd' is not defined                                              no-undef
  Line 1:2229:  Expected an assignment or function call and instead saw an expression  no-unused-expressions
  Line 1:2312:  Expected an assignment or function call and instead saw an expression  no-unused-expressions
  Line 1:2369:  'getKaiAd' is not defined                                              no-undef
  Line 1:2420:  Expected an assignment or function call and instead saw an expression  no-unused-expressions
  Line 1:2430:  'getKaiAd' is not defined                                              no-undef
  Line 1:2466:  'getKaiAd' is not defined                                              no-undef

Search for the keywords to learn more about each error.

Solution

  • The solution is:

    First, add this inside index.html

    <script type="text/javascript" src="libs/kaiads.v4.min.js" defer></script>
    

    Then, call the function like this: window.getKaiAd