I publish react component in npm and I import in other project but is error,how to solve ?
See original GitHub issueA project:
import cdPlayer from '../assets/css/CdPlayer.css'
import React from 'react'
import PropTypes from 'prop-types'
import FastForward from 'material-ui-icons/FastForward';
import FastRewind from 'material-ui-icons/FastRewind';
import { LinearProgress } from 'material-ui/Progress';
import Cd from '../components/Cd'
class ReactCdPlayer extends React.Component{
constructor(props){
super(props)
this.state={
audio:null,
listStatus : 0,
isPlaying :false,
currentMusic : 0,
currentMusicItem:null,
prevMusic : -1,
nextMusic:0,
repeat : 2,
relist :['fa-random', 'fa-refresh', 'fa-retweet'],
retitle : ['Random', 'Cycle', 'Order'],
completed: 0,
progress:null
}
}
componentDidMount() {
if (localStorage.currentMusic)
{
this.setState({
currentMusic: parseInt(localStorage.currentMusic)
})
}
if (this.state.currentMusic >= this.props.musicList.length) {
localStorage.currentMusic = 0;
this.setState({
currentMusic: parseInt(localStorage.currentMusic)
})
window.location.reload();
}
this.loadMusic(this.state.currentMusic);
this.state.audio.addEventListener('timeupdate',this.onTimeUpdate)
}
onTimeUpdate=()=>{
console.log('ontimeupdate')
const ratio = this.state.audio.currentTime / this.state.audio.duration * 100;
this.setState({
completed:ratio
})
if (parseInt(ratio) == 100) {
this.onFastForward();
}
}
loadMusic = (i)=> {
localStorage.currentMusic = i;
this.setState({
currentMusic: parseInt(localStorage.currentMusic),
currentMusicItem:this.props.musicList[i],
completed:0
}, () => {
if(this.state.isPlaying){
this.play();
}
})
}
progress = () => {
if (this.state.completed > 100) {
this.setState({ completed: 100});
} else {
const diff = Math.random() * 10;
this.setState({ completed: this.state.completed + diff });
}
}
onProgress=(e)=>{
const distance = e.pageX - this.state.progress.offsetLeft;
this.state.audio.currentTime=distance * (this.state.audio.duration / this.state.progress.offsetWidth)
}
play = ()=>{
this.state.audio.play();
this.setState({
isPlaying:true
})
//call back to parent
if(typeof this.props.onPlay !== 'undefined')
{
this.props.onPlay()
}
}
pause=()=>{
this.state.audio.pause();
this.setState({
isPlaying:false
});
//call back to parent
if(typeof this.props.onPause !== 'undefined')
{
this.props.onPause()
}
}
onPlay=(isPlaying)=>{
this.play()
}
onPause=(isPlaying)=>{
this.pause()
}
//上一曲
onFastRewind=()=>{
console.log('onFastRewind music')
if (this.state.prevMusic != -1) {
this.loadMusic(this.state.prevMusic);
// prevMusic = randomNum(0, this.state.playlist.length);
} else if (this.state.currentMusic == 0) {
this.loadMusic(this.props.musicList.length - 1);
} else {
this.loadMusic(parseInt(this.state.currentMusic) - 1);
}
}
//下一曲
onFastForward=()=>{
console.log('onFastForward music')
if (this.state.repeat == 0) {
this.setState({
prevMusic:this.state.currentMusic
})
this.setState({
nextMusic:this.randomNum(0, this.props.musicList.length)
})
this.loadMusic(this.state.nextMusic);
} else if (this.state.currentMusic == this.props.musicList.length - 1) {
this.loadMusic(0);
} else {
this.loadMusic(parseInt(this.state.currentMusic) + 1);
}
}
randomNum = (min, max)=> {
return Math.floor(parseInt(min) + Math.random() * parseInt(max - min));
};
render(){
return(
<section className='cd-player-container'>
<section className="control-buttons">
<section className='fast-rewind-outer'>
<FastRewind className='fast-rewind-icon' onClick={this.onFastRewind}/>
</section>
<section className="cd-outer">
<Cd {...this.state} onPlay={this.onPlay} onPause={this.onPause}/>
</section>
<section className='fast-forward-outer'>
<FastForward className='fast-forward-icon' onClick={this.onFastForward}/>
</section>
</section>
<section className='title'>
<h1 className='name'>{this.state.currentMusicItem?this.state.currentMusicItem.title:''}</h1>
<h2 className="sub-title">{this.state.currentMusicItem?this.state.currentMusicItem.artist:''}</h2>
</section>
<audio ref={ref=>this.state.audio = ref} id="music" src={this.state.currentMusicItem?this.state.currentMusicItem.mp3:''}></audio>
<section className="process-outer" ref={ref=>this.state.progress = ref} >
<LinearProgress color="accent" mode="determinate" value={this.state.completed} onClick={this.onProgress}/>
</section>
</section>
)
}
}
ReactCdPlayer.propTypes={
musicList:PropTypes.array.isRequired,
onPlay:PropTypes.func
}
export default ReactCdPlayer
{
"name": "react-cd-player",
"homepage": "http://zhangwei900808.github.io/react-cd-player",
"version": "0.3.0",
"main": "src/containers/ReactCdPlayer.js",
"dependencies": {
"classnames": "^2.2.5",
"material-ui": "next",
"material-ui-icons": "^1.0.0-alpha.19",
"node-sass-chokidar": "^0.0.3",
"npm-run-all": "^4.0.2",
"react": "^15.6.1",
"react-dom": "^15.6.1"
},
"devDependencies": {
"gh-pages": "^1.0.0",
"react-scripts": "1.0.11"
},
"scripts": {
"build-css": "node-sass-chokidar src/assets/css/ -o src/assets/css/",
"watch-css": "npm run build-css && node-sass-chokidar src/assets/css/ -o src/assets/css/ --watch --recursive",
"start-js": "react-scripts start",
"start": "npm-run-all -p watch-css start-js",
"build": "npm run build-css && react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject",
"predeploy": "npm run build",
"deploy": "gh-pages -d build"
}
}
B project
import CdPlayer from 'react-cd-player'
<CdPlayer musicList={musicList}></CdPlayer>
Issue Analytics
- State:
- Created 6 years ago
- Comments:7
Top Results From Across the Web
Publish React components as an npm package | by JB
I found myself copy-pasting my React components from project to project and wanted to create an npm package so I could import them...
Read more >Duplicate ReactJS import issue when using npm link to test ...
These configs work like charm when I publish my component to NPM and install it in my another ReactJs project with `npm install...
Read more >Deploy React Component As An NPM Library - codeburst
NPM comes with Node.js , it does need a separate installation. Project setup. Let's set up our project directory. I'll call mine countdown- ......
Read more >The complete guide to publishing a React package to npm
So you want to publish a React component as a package to npm? This guide has all the information you need, even if...
Read more >Vue.js - How to Build Your First Package & Publish It on NPM
Import it and call the Vue.use() global method. ... There is also another way to add a new plugin when the package author...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This questions are not related to the create-react-app. Use Stackoverflow.
@miraage Ok! and very Thank you