【发布时间】:2023-04-17 22:17:01
【问题描述】:
虽然我已经在 reactjs 组件(名称为 renderLocationLink 的组件)的 render 方法返回的 html 中包含了 a 标签的 onclick 处理程序,但渲染正确发生在 onclick 处理程序属性没有出现在网页上呈现的 html 中。我想要无法弄清楚是什么问题,这是代码
var feedApp = React.createClass({
getInitialState: function(){
return {
data : [
{display_name:"Rao",content:"this is Rao post",links:['link1','link2','link3']},
{display_name:"Sultan",content:"this is Sultans",links:['link4','link5','link6']},
{display_name:"John",content:"this is John post",links:['link7','link8','link9']}
]
}
},
fetchFeedsFromUrl: function(){
console.log('Onclick triggered');
},
render: function(){
return (<Feeds data={this.state.data} onClick={this.fetchFeedsFromUrl} />)
}
})
var Feeds = React.createClass({
render: function(){
var onClickfunc = this.props.onClick;
var feeds = this.props.data.map(function(feed){
return (
<oneFeed name={feed.display_name} onClick={this.onClickfunc} content={feed.content} links={feed.links} />
)
});
return(
<div> {feeds} </div>
)
}
})
var oneFeed = React.createClass({
render: function() {
return (
<div>
<h3>{this.props.name}</h3>
<renderLocationLink onClick={this.props.onClick} linkArray={this.props.links} />
<p>{this.props.content} </p>
</div>
)
}
});
var renderLocationLink = React.createClass({
render: function(){
var onClick = this.props.onClick;
var locationLinks = this.props.linkArray.map(function(link,index){
return (<a onClick={this.onClick} href={link}>{link} </a>)
})
return ( <div >{locationLinks}</div> )
}
})
React.renderComponent(feedApp(null),document.body);
【问题讨论】:
-
是
onClick属性在您呈现的标记中不存在的问题,还是处理程序似乎不起作用的问题? -
@DanielApt ,这两种情况都是正确的,onClick 不存在于呈现的标记中并且它也不起作用....
-
好吧,它永远不会出现在渲染的标记中。您可能只是到目前为止将属性向下传递,并且该属性永远不会到达预期的子组件。
标签: javascript reactjs