简体   繁体   中英

Javascript: How do I use React component globally?

I've made an NPM package for react, it's working fine on Node but it's not working on browser without node.

if I import within node like this:

import Progress from 'package-name'

// jsx
<Progress /> //working fine

It's working fine.

But if I use it from CDN like unpkg, it's not working.

<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.4.2/react.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.4.2/react-dom.js"></script>

// Package Script
<script src="https://unpkg.com/@delowar/react-circle-progressbar@0.0.9/lib/Progress.js"></script>

<script>
    ...react code...
    <Progress />
    ...react code...
</script>

It's showing an error:

Progress is not defined

在此处输入图片说明 Can anyone please help me about this issue?

Webpack Configuration:

var path = require('path');

module.exports = {
    mode: 'production',
    entry: './src/Progress.js',
    output: {
        path: path.resolve('lib'),
        filename: 'Progress.js',
        libraryTarget: 'umd',
        library: 'lib',
        umdNamedDefine: true,
        globalObject: `(typeof self !== 'undefined' ? self : this)`
    },
    module: {
        rules: [
            {
                test: /\.(js|jsx)$/,
                exclude: /(node_modules)/,
                use: 'babel-loader'
            }
        ]
    }
}

Original Repo: https://github.com/delowardev/react-circle-progressbar

Change the value of library to the name of your component library: 'Progress' and add libraryExport: 'default' to assign the default export to the library target:

output: {
  path: path.resolve('lib'),
  filename: 'Progress.js',
  library: 'Progress',
  libraryTarget: 'umd',
  libraryExport: 'default',
  umdNamedDefine: true,
  globalObject: `(typeof self !== 'undefined' ? self : this)`
},

Working example :

 function App() { return ( <Progress /> ) } ReactDOM.render( <App /> , document.getElementById('root'))
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.4.2/react.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.4.2/react-dom.js"></script> <script src="https://rawcdn.githack.com/fraction01/react-circle-progressbar/0957fed54db16a3f7b9d625711ed3961f3b34371/lib/Progress.js"></script> <div id="root"></div>

You should configure it as external dependency as described in the docs .
Add this to your webpack configuration:

externals : {
    Progress: 'progress'
}

Usage:
import Progress from 'progress';

You can't require or import modules directly from URL.

But you can import it like below.

// Package Script
<script src="https://unpkg.com/@delowar/react-circle-progressbar@0.0.9/lib/Progress.js"></script>

const Progress = window.Progress;

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