2017-04-25 27 views
6

私はrefを使用して何かをしようとしていません。要素がキャンバスなので、要素への参照が必要です。キャンバス上に描画するためには、そのrefが必要です。 (技術的に動作しますが、奇妙な感じ)私が試しReact - 親コンポーネントの子コンポーネントにあるrefを取得します

class Parent extends Component { 
    clickDraw =() => { 
    // when button clicked, get the canvas context and draw on it. 
    // how? 
    } 

    render() { 
    return (
     <div> 
     <button onClick={this.clickDraw}> Draw </button> 
     <Child /> 
     </div> 
    ); 
    } 
} 


class Child extends Component { 
    componentDidMount() { 
    const ctx = this.canvas.getContext('2d'); 
    // draw something on the canvas once it's mounted 
    ctx.fillStyle = "#FF0000"; 
    ctx.fillRect(0,0,150,75); 
    } 

    render() { 
    return (
     <canvas width={300} 
       height={500} 
       ref={canvasRef => this.canvas = canvasRef}> 
     </canvas> 
    ); 
    } 
} 

=====

何かが親で<canvas>を定義するので、そのREFの関数で、this親成分を意味します。次に、<canvas>this.canvasを2つの別々の小道具として子供に渡します。子のレンダリング機能に<canvas>(名前はthis.props.canvasJSX)を返し、そのコンテキストを取得するためにthis.canvas(名前はthis.props.canvasRef)を使用します。下記を参照してください。

class Parent extends Component { 
    clickDraw =() => { 
    // now I have access to the canvas context and can draw 
    const ctx = this.canvas.getContext('2d'); 
    ctx.fillStyle = "#00FF00"; 
    ctx.fillRect(0,0,275,250); 
    } 

    render() { 
    const canvas = (
     <canvas width={300} 
       height={500} 
       ref={canvasRef => this.canvas = canvasRef}> 
     </canvas> 
    ); 
    return (
     <div> 
     <button onClick={this.clickDraw}> Draw </button> 
     <Child canvasJSX={canvas} 
       canvasRef={this.canvas} /> 
     </div> 
    ); 
    } 
} 


class Child extends Component { 
    componentDidMount() { 
    const ctx = this.props.canvasRef.getContext('2d'); 
    // draw something on the canvas once it's mounted 
    ctx.fillStyle = "#FF0000"; 
    ctx.fillRect(0,0,150,75); 
    } 

    render() { 
    return this.props.canvas; 
    } 
} 

もっと標準的な方法はありますか?

+0

[親コンポーネントの子コンポーネントのリファレンスにアクセスするにはどうすればよいですか](https://stackoverflow.com/questions/37647061/how-do-i-access-refs-of-a-child)親コンポーネントのコンポーネント) – arpl

+0

getterとsetterを親コンポーネントで使用してください。コンポーネント – zloctb

答えて

4

あなたが実際には最初のアプローチを使用する必要があり、あなたが親に子要素、参考文献にアクセスすることができます

class Parent extends Component { 
    clickDraw =() => { 
    // when button clicked, get the canvas context and draw on it. 
    const ctx = this.childCanvas.canvas.getContext('2d'); 
    ctx.fillStyle = "#00FF00"; 
    ctx.fillRect(0,0,275,250); 
    } 

    render() { 
    return (
     <div> 
     <button onClick={this.clickDraw}> Draw </button> 
     <Child ref={(ip) => this.childCanvas = ip}/>; 
     </div> 
    ); 
    } 
} 


class Child extends Component { 
    constructor() { 
    super(); 
    this.canvas = null; 
    } 
    componentDidMount() { 
    const ctx = this.canvas.getContext('2d'); 
    // draw something on the canvas once it's mounted 
    ctx.fillStyle = "#FF0000"; 
    ctx.fillRect(0,0,150,75); 
    } 

    render() { 
    return (
     <canvas width={300} 
       height={500} 
       ref={canvasRef => this.canvas = canvasRef}> 
     </canvas> 
    ); 
    } 
} 

にのみ、このアプローチを使用することができる子コンポーネントがclassとして宣言されています。

+0

ありがとう!私が編集したわずかなタイプミス( 'refs'は' ref'でなければなりません)を除いて完璧です。 – tscizzle

+0

Shubham私はこの実装を複数のHTML要素[私のプロジェクト](https://stackblitz.com/jp/react-pulnct?file=Posts.js) – Omar

+1

@Omar、あなたのスニペットを編集しましたhttps://stackblitz.com/edit/react-yqcgey?file=Posts.js –

0

それはReact docsから抽出された提案されたパターンを避けることができない場合は、次のようになります。

import React, {Component} from 'react'; 

const Child = ({setRef}) => <input type="text" ref={setRef} />; 

class Parent extends Component { 
    constructor(props) { 
     super(props); 
     this.setRef = this.setRef.bind(this); 
    } 
    componentDidMount() { 
     // Call function on Child dom element 
     this.childInput.focus(); 
    } 
    setRef(input) { 
     this.childInput = input; 
    } 
    render() { 
     return <Child setRef={this.setRef} /> 
    } 
} 

年代thisにバインドされた小道具としての機能を渡します。応答はrefコールバックsetRefと呼ばれ、childInputのプロパティはthisに添付されています。これはすでに述べたようにの親です。

関連する問題