2016-10-06 5 views
0

私は反応があり、状態が変化するたびにキャンバスを反応させてレンダリングしようとしています。リアクションレンダリング/更新キャンバス

//set initial state 
constructor(props) { 
    super(props); 
    this.state = { 
     imgFile: this.props.imgFile, 
     show: false 
    }; 
} 

//The render canvas 
render() { 
    return (
     <div className="col-sm-4"> 
     <canvas ref={(c) => { this.myCanvas = c; }} /> 
     </div> 
    ); 
    } 

私の質問です:小道具でキャンバスを更新する方法はありますか?これまでのところ、私はこれだ:

componentDidMount() { 
    const origin = this.state.imgFile; 
    // canvas area/img calculations 
    this.funcForDrawCanvas(origin.path, origin.presition, origin.height, origin.width); 
} 

をそれがマウントされてコンポーネントきた後、それ以上の任意のアイデアを、それを更新しませんか?またはキャンバスタグを削除し、状態が変更されたときに再度挿入する方法はありますか?

+1

各コンポーネントの仕様が何であるかを必ず確認してください。これを達成する方法はたくさんありますが、['componentDidMount'](https://facebook.github.io/react/docs/component-specs.html#mounting-componentdidmount)でキャンバスを初期化する方法、 ['shouldComponentUpdate'](https://facebook.github.io/react/docs/component-specs.html#updating-shouldcomponentupdate)、[' componentDidUpdate'](https://facebook.github.io)でキャンバスレンダリングを行います/react/docs/component-specs.html#updating-componentdidupdate)。 – Dom

+0

@Dom imgでキャンバスを再描画する方法の例を教えていただけますか? –

答えて

1

componentWillReceivePropsを使用して、小道具の変化を検出し、それを使用してキャンバスに描画します。

関連する問題