【问题标题】:React - Get the ref present in child component in the parent componentReact - 获取父组件中子组件中存在的引用
【发布时间】:2017-04-25 04:11:33
【问题描述】:

我并不想使用 refs 做任何骇人听闻的事情。我只需要元素的引用,因为元素是画布,要在画布上绘图,您需要它的引用。

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>
    );
  }
}

=====

我尝试过(技术上可行但感觉很奇怪)的方法是在父组件中定义 &lt;canvas&gt;,因此在其 ref 函数中,this 指的是父组件。然后我将&lt;canvas&gt; 和this.canvas 作为两个单独的道具传递给孩子。我在孩子的渲染函数中返回&lt;canvas&gt;(命名为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;
  }
}

有没有更标准的方法来实现这一点?

【问题讨论】:

标签: javascript reactjs canvas


【解决方案1】:

您实际上应该使用第一种方法,并且可以访问父元素中的子元素 refs

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。

【讨论】:

【解决方案2】:

如果无法避免,从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} />
    }
}

Parent 传递一个函数作为绑定到Parent 的this 的道具。 React 将调用 Child 的 ref 回调 setRef 并将 childInput 属性附加到 this,正如我们已经指出的那样,它指向 Parent。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-25
    • 2016-07-01
    • 2020-02-12
    • 2021-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多