2017-11-19 1 views
0

からコンポーネントを呼び出すと、私はWebPACKのか、などのように私のアプリで任意のバンドルを使用していない...とも作り方reactjsは、私は、外部ファイルからこのコンポーネントを実行するように反応するアプリを作りたい外部ファイル

が反応します/app.js

import React, { Component } from 'react'; 
import logo from './logo.svg'; 
import './App.css'; 
import BounceCircle from './canvas'; 

class App extends Component { 

    render() { 
    return (
     <div className="App"> 
     <Header/> 
     <Content/> 
     <Footer/> 
     <BounceCircle/> 
     </div> 
    ); 
    } 
} 

class Header extends Component { 
    render() { 
    return (
     <div className="App-header"> 
      <img src={logo} className="App-logo" alt="logo" /> 
      <h1>Hello World</h1> 
      <h4>hello to egypt</h4> 
      <h3>Welcome to React</h3> 
      <h4>hi</h4> 
     </div> 

    ); 
    } 
} 

class Content extends Component { 
    render() { 
    return (

      <p className="App-intro"> 
       To get started, edit <code>src/App.js</code> and save to reload. 
       </p> 

    ); 
    } 
} 

class Footer extends Component { 
    render() { 
    return ( 
      <h3>this the Footer</h3> 

    ); 
    } 
} 






export default App; 

、ここに私はそれを輸入することとで仕事をしたいアプリを反応させるコンポーネントのコードは、基本的に私はそれがで実行させようHTML5のキャンバスでは、アプリ

キャンバスを反応させます。 js

外部ファイルがあるから

import BounceCircle from './canvas' 

をそしてあなたが他のコンポーネントと同じように使用することができます<BounceCircle />

インポート:

class BounceCircle extends Component { 
    render() { 
    return (
      //this var = canvas is to call the canvas tag from html file using querySelector 
      canvas=document.querySelector('canvas'); 
      //this makes the width of the canvas the full width the screen 
      canvas.width = window.innerWidth; 
      //this makes the height of the canvas the full height the screen 
      canvas.height=window.innerHeight; 
      // this var = c is to call var canvas to start to work on it 
      var c=canvas.getContext("2d"); 


      var mouse = { 
       x:undefined, 
       y:undefined 
      } 

      var maxRadius = 40; 
      var colorArray=[ 
       '#FF595E', 
       '#33032F', 
       '#313E50', 
       '#0E7C7B', 
       '#87BCDE', 
      ]; 
      window.addEventListener('mousemove', 
       function(event){ 
        mouse.x=event.x; 
        mouse.y=event.y; 
      }) 

      window.addEventListener('resize',function() 
      { 
       //this makes the width of the canvas the full width the screen 
       canvas.width = window.innerWidth; 
      //this makes the height of the canvas the full height the screen 
       canvas.height=window.innerHeight; 

       init(); 
      }) 
      function Circle(x,y,dx,dy,radius){ 
       this.x = x; 
       this.y = y; 
       this.dx= dx; 
       this.dy= dy; 
       this.radius=radius; 
       this.minRadius=radius; 
       this.color=colorArray[Math.floor(Math.random()*colorArray.length)]; 
       this.draw = function() 
       { 

        c.beginPath(); 
        c.arc(this.x,this.y,this.radius,0,Math.PI * 2,false); 
        c.fillStyle=this.color; 
        c.strokeStyle=this.color; 
        c.fill(); 
       } 
       this.update = function(){ 
        if (this.x + this.radius> innerWidth || this.x-this.radius < 0) { 
       this.dx = -this.dx ; 
      } 

      if (this.y + this.radius> innerHeight || this.y-this.radius < 0) { 
       this.dy = -this.dy ; 
      } 
       this.x += this.dx; 
       this.y += this.dy; 

       //interactivity 
       if (mouse.x - this.x <50 && mouse.x-this.x>-50 && mouse.y - this.y <50 && mouse.y-this.y>-50) { 
        if (this.radius<maxRadius) { 
         this.radius+= 1; 
        } 

       } else if (this.radius>this.minRadius) { 
        this.radius-= 1; 
       } 
       this.draw(); 
       } 
      } 


      var circleArray=[]; 
       for (var i = 0; i < 500; i++) 
      { 
       var radius=Math.random() * 3 + 1; 
       var x =Math.random()*innerWidth; 
       var y = Math.random()*innerHeight; 
       var dx= (Math.random() - 0.5)*16; 
       var dy= (Math.random() - 0.5)*16; 


       circleArray.push(new Circle(x,y,dx,dy,radius)); 

      } 

      function init() 
      { 
      circleArray=[]; 

      } 


      function animate(){ 
       requestAnimationFrame(animate); 
       c.clearRect(0,0,innerWidth,innerHeight); 

      for (var i = 0; i < circleArray.length; i++) 
      { 
       circleArray[i].update(); 
      } 

      } 

      animate(); 



    ); 
    } 
} 

export default BounceCircle; 

両方のファイルが同じフォルダにapp.jsあなたが言うことができるで

+0

'輸入のApp "./app.js"'から?あなたが試したこととうまくいかなかったことについてもっと正確にしてください。 –

+0

@Damien Leroux 3、いいえ、私は反対をしたいです。私はcanvas.jsからapp.jsにインポートします。 –

答えて

0

ですnode_modulesからのインポートのようなものです。ちょうど正しいディレクトリを指してください。

ファイルには、default exportがあります。 app.jsに好きなようにdefault exportsという名前を付けることができます。だから、これらすべてが動作します、と同じこと(BounceCircle)を返します:よう

import Cheese from './canvas'; 
import Canvas from './canvas'; 
import MyThingy from './canvas'; 

その他の輸出:

`myCode.js` 

export class Pies extends Component { /* code */ } 

class Cake extends Component { /* code */ } 

default export Cake; 

は、今、私たちはPiesのデフォルトの輸出を持っているが、我々はまた、持っていますexport class(デフォルトではない)

ものではないデフォルトがカーリーを使用してインポートすることができます。

import { Pies } from './myCode' 

curliesの値は、インポートされたファイルの名前と常に一致する必要があります。

しかし、それはまだ同じことにつながるおり、このことは可能であろう:

import Cake, { Pies } from './myCode' 
import ICanUseEverythingHere, { Pies } from './myCode' 
+0

ここに画面が表示されます。コンパイルします。 ./src/canvas.jsのエラー 構文エラー:Git/React App/my-app/src/canvas.jsのD:/ My WebSites:予期しないトークン(4:4) 2 | 3 | \t //このvar = canvasは、querySelectorを使用してhtmlファイルからcanvasタグを呼び出すことです > 4 | var canvas = document.querySelector( 'canvas'); |^ 5 | //キャンバスの幅を画面の全幅に合わせる 6 | canvas.width = window.innerWidth; | 7 | //キャンバスの高さを画面全体の高さにします @ ./src/App.js 20:14-33' –

+0

これは、インポートが成功したことを意味しますが、インポートしようとしているコードには失敗。return文の中に変数を設定することは、Reactコンポーネントの動作でもありません。 'render()'のreturn文にはdomノードだけが含まれていなければなりません。あなたはおそらくこのコードをどこかからコピーしましたか?あなたはReactのためにそれを書き直すべきです。 – Hespen

+0

どのように変数を反応の良い仕事に変えるか –

関連する問題