[英]React: How to render same component with same onClick and different drops
I'm trying to use React Components to render all the projects I have in a .json. 我正在尝试使用React Components渲染.json中的所有项目。 This is my code so far:
到目前为止,这是我的代码:
class App extends Component {
constructor(props){
super(props);
this.state = {
load: '',
projectInfo:{
"projects":[]
}
}
}
getProjectInfo(){
$.ajax({
url:'http://localhost:3000/projectInfo.json',
dataType:'json',
cache: false,
success: function(data){
this.setState({projectInfo: data});
}.bind(this),
error: function(xhr, status, err){
console.log(err);
alert(err);
}
});
}
componentDidMount(){
this.getProjectInfo();
}
handleClick = (value) => {
this.setState({load: value});
}
display(app){
if(app == 'bmi') {
return <Bmi />
}
if (app === 'js') {
return <Js />
}
else return "";
}
render() {
if(this.state.projectInfo){
var projects = this.state.projectInfo.projects.map(function(project){
return (<Project id={project.id}
projectName={project.name}
projectDescription={project.description}
imgAddress={project.imgAddress}
onClick={this.handleClick}/>)
})
}
return (
<div className="App">
<div className="App-header">
<h2>React Test</h2>
</div>
<div className="box">
{projects}
</div>
<div className={this.state.load}>
{this.display(this.state.load)}
</div>
</div>
);
}
}
export default App;
I'm having trouble with the onClick, getting the TypeError: Cannot read property 'handleClick' of undefined. 我在onClick上遇到麻烦,遇到TypeError:无法读取未定义的属性'handleClick'。 I think the problem is that when I use {this.handleClick}, 'this' refers to 'project' passed to the function, but I'm not sure how to get around it.
我认为问题在于,当我使用{this.handleClick}时,“ this”是指传递给函数的“项目”,但是我不确定如何解决它。
This was working as desired when I manually listed all the Project Components to be rendered. 当我手动列出所有要渲染的项目组件时,这按预期工作。
You did not bind the map for the context of this
, but since you are using () =>
notation you can make use of the auto bind eg 你没有在地图的背景下结合
this
,但因为使用的是() =>
符号,你可以使用自动绑定例如
var projects = this.state.projectInfo.projects.map(project => {
return (<Project id={project.id}
projectName={project.name}
projectDescription={project.description}
imgAddress={project.imgAddress}
onClick={this.handleClick}/>)
})
EDIT: 编辑:
To pass more arguments you have to use a callback for onClick
eg 要传递更多参数,您必须对
onClick
使用回调,例如
onClick={() => this.handleClick(project.id)}
Save this.handleClick
to a variable before jump into map
将
this.handleClick
保存到变量,然后再跳转到map
if(this.state.projectInfo){
var handle = this.handleClick
var projects = this.state.projectInfo.projects.map(function(project){
return (<Project id={project.id}
projectName={project.name}
projectDescription={project.description}
imgAddress={project.imgAddress}
onClick={handle}/>)
})
}
You need to bind to the correct context.Either you can do this
您需要绑定到正确的上下文。
onClick={this.handleClick.bind(this)}
OR 要么
In constructor
在构造函数中
constructor(props){
super(props);
this.state = {
load: '',
projectInfo:{
"projects":[]
}
};
this.handleClick = this.handleClick.bind(this);
}
内部构造函数的绑定此的功能
this.handleClick = this.handleClick.bind(this);
Hi you have problem with context. 嗨,你有上下文的问题。 My suggestion is create proper function to handle click
我的建议是创建适当的功能来处理点击
handleClick(value){
this.setState({load: value});
}
than add lambda function to: 比将lambda函数添加到:
if(this.state.projectInfo){
var projects = this.state.projectInfo.projects.map((project) => {
return (<Project id={project.id}
projectName={project.name}
projectDescription={project.description}
imgAddress={project.imgAddress}
onClick={()=>{this.handleClick(project.id);}}/>)
})
}
I am not sure what kind of parameter you need to pass to handle click function but i guess that this is project.id 我不确定您需要传递哪种参数来处理点击功能,但我想这是project.id
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.