我正在使用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