我正在使用React和Raphael進行一個項目,而且我正面臨一個我無法解決的上下文問題。React - 上下文問題 - raphael反應
代碼的簡化版本如下:
import React from 'react';
import {Raphael,Paper,Set,Circle,Ellipse,Image,Rect,Text,Path,Line} from 'react-raphael';
export default class PlanCdf extends React.Component {
\t onmouseover(){
\t \t console.log(this);
\t \t console.log(this.props.project);
\t }
\t drawTasks() {
\t \t return this.props.tasks.map((task) => {
\t \t \t return <Path mouseover={this.onmouseover} key={task._id} d={coords} attr={{"fill":"#444444", "stroke-width" : "2"}} />
\t \t })
\t }
\t render() {
\t \t return(
\t \t \t <Paper className={"paper"} width={this.state.paperWidth} height={this.state.paperHeight}>
\t \t \t \t <Set>
\t \t \t \t \t { this.drawTasks() }
\t \t \t \t </Set>
\t \t \t </Paper>
\t \t)
\t }
}
在我的onmouseover功能,我想訪問兩個方面:爲了訪問狀態和道具的一般情況和Path上下文爲了在onmouseover時改變路徑的屬性,但不知何故,我總是設法得到兩個中的一個。
我從路徑中獲取這個上下文(如上例所示),但無法訪問this.state。 或者如果我綁定我得到完整的,但不能訪問我的路徑了。 。
也許簡單的綁定問題我不明白,但不能管理,以找到一個解決方案
---編輯---
這就是我目前的處境:
import React from 'react';
import {Raphael,Paper,Set,Circle,Ellipse,Image,Rect,Text,Path,Line} from 'react-raphael';
export default class PlanCdf extends React.Component {
constructor(props) {
super(props);
this.drawTasks = this.drawTasks.bind(this);
}
onmouseover(){
console.log(this.state);
}
drawTasks() {
return this.props.tasks.map((task) => {
return <PathWrapper mouseover={this.onmouseover} key={task._id} d={coords} attr={{"fill":"#444444", "stroke-width" : "3"}} />
})
}
render() {
return(
<Paper className={"paper"} width={this.state.paperWidth} height={this.state.paperHeight}>
<Set>
{ this.drawTasks() }
</Set>
</Paper>
)
}
}
export class PathWrapper extends React.Component {
constructor(props){
super(props);
}
onmouseover() {
console.log(this)
this.attr({'stroke-width':'5'})
this.props.mouseover()
}
render() {
return <Path mouseover={this.onmouseover} d={this.props.d} attr={this.props.attr} />
}
}
按照建議,我可以在新的PathWrapper組件中更改attr。但我仍然無法進入該州。我試圖調用發送到道具的功能來訪問父功能,但我不能,因爲我需要在路徑上下文來改變屬性......我或多或少地將pb移動到子組件
我覺得我們是在正確的方向,但它是sitll不工作。我正在編輯相應的問題 – Ivo