reactjs - How to include external javascript library in PreactJS? - Stack Overflow

I'm currently one small project based on preactJS with algolia? I found that there is algolia pone

I'm currently one small project based on preactJS with algolia? I found that there is algolia ponent for reactjs but unfortunately, no algolia ponent for preactjs. That's why my problem is how can I include algolia javascript file into preactjs?

<script src=".js/1/instantsearch.min.js"></script>

I'm currently one small project based on preactJS with algolia? I found that there is algolia ponent for reactjs but unfortunately, no algolia ponent for preactjs. That's why my problem is how can I include algolia javascript file into preactjs?

<script src="https://cdn.jsdelivr/instantsearch.js/1/instantsearch.min.js"></script>
Share Improve this question edited Aug 16, 2017 at 9:11 Pyae Phyoe Shein asked Aug 16, 2017 at 8:35 Pyae Phyoe SheinPyae Phyoe Shein 13.8k47 gold badges152 silver badges243 bronze badges 4
  • Also, I don't know if you see it but we have another flavor of InstantSearch, React-InstantSearch, that might integrate better with your preact app. Don't hesitate to give us your feedback about it. – Marie Commented Aug 16, 2017 at 11:25
  • @Marie I've tried with React-InstantSearch but when I rendered, I've encountered process error. That's why I've gave it up. – Pyae Phyoe Shein Commented Aug 17, 2017 at 4:47
  • Could you open an issue on our github repository with the error you encountered? We need to investigate about that :) – Marie Commented Aug 17, 2017 at 7:55
  • @Marie sure, I've created ticket. github./algolia/react-instantsearch/issues/264 – Pyae Phyoe Shein Commented Aug 18, 2017 at 2:28
Add a ment  | 

4 Answers 4

Reset to default 4

Preact has ponent Head. To install it run

yarn add preact-head

Then it will be possible to specify standard head element such as script, meta, etc.. E.x. app.js

import Head from "preact-head";

export default class App extends Component {

        render() {
            return (
                <div id="app">
                    <Head>
                        <meta name="Whatever" />
                        <script src="https://cdn.jsdelivr/instantsearch.js/1/instantsearch.min.js"></script>
                    </Head>

                </div>
            );
        }
    }

You can insert script dynamically, try this one:

ponentWillMount() {
    const script = document.createElement("script");
    script.src = "https://cdn.jsdelivr/instantsearch.js/1/instantsearch.min.js";
    script.async = true;

    script.onload = function() {
       // init your algolia code here
    }

    document.body.appendChild(script);
},

You can install the package algoliasearch (if package is not correct, just search the desired package at https://npmjs.)

Now you can do npm install algoliasearch --save at your root level where package.json is there and use it in your module like following -

import algolia from 'algolia';

And then use its methods in your code.

You can just add it in your index.html between the head tags. In React, the index.html is in public/index.html In Preact, index.html is in src/index.html

<head>
<script src="https://cdn.jsdelivr/instantsearch.js/1/instantsearch.min.js"></script>
</head>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744795346a4594173.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信