我试图实现悬停事件但是onMouseLeave并不总是在离开元素时触发,特别是在快速移动光标元素时.我试过Chrome,Firefox和Internet Explorer,但在每个浏览器中都出现了同样的问题.
我的代码:
import React from 'react';
import Autolinker from 'autolinker';
import DateTime from './DateTime.jsx'
class Comment extends React.Component{
constructor(props){
super(props);
this.handleOnMouseOver = this.handleOnMouseOver.bind(this);
this.handleOnMouseOut = this.handleOnMouseOut.bind(this);
this.state = {
hovering: false
};
}
render(){
return <li className="media comment" onMouseEnter={this.handleOnMouseOver} onMouseLeave={this.handleOnMouseOut}>
<div className="image">
<img src={this.props.activity.user.avatar.small_url} width="42" height="42" />
</div>
<div className="body">
{this.state.hovering ? null : <time className="pull-right"><DateTime timeInMiliseconds={this.props.activity.published_at} byDay={true}/></time>}
<p>
<strong>
<span>{this.props.activity.user.full_name}</span>
{this.state.hovering ? <span className="edit-comment">Edit</span> : null}
</strong>
</p>
</div>
</li>;
}
handleOnMouseOver(event){
event.preventDefault();
this.setState({hovering:true});
}
handleOnMouseOut(event){
event.preventDefault();
this.setState({hovering:false}); …
Run Code Online (Sandbox Code Playgroud) 我有这样的组件:
import React from 'react';
import Autolinker from 'autolinker';
class Comment extends React.Component{
constructor(props){
super(props);
}
render(){
return <li className="media comment">
<div className="image">
<img src={this.props.activity.user.avatar.small_url} width="42" height="42" />
</div>
<div className="body">
<p>
<strong>{this.props.activity.user.full_name}</strong>
</p>
</div>
<div>
<p>
{Autolinker.link(this.props.activity.text)}
</p>
</div>
</li>;
}
}
export default Comment;
Run Code Online (Sandbox Code Playgroud)
Autolinker返回一个字符串值,如下所示:
"So basically <a href="http://www.silastar.com/dev-sila" target="_blank">silastar.com/dev-sila</a> is perfect and works correctly?"
Run Code Online (Sandbox Code Playgroud)
如何将此字符串转换为html JSX,以便锚链接显示为链接而不是纯文本?
我试图通过this.refs调用子组件内的函数,但我不断收到此函数不存在的错误.
Uncaught TypeError: this.refs.todayKpi.loadTodaysKpi is not a function
Run Code Online (Sandbox Code Playgroud)
父组件:
class KpisHeader extends React.Component {
constructor() {
super();
this.onUpdate = this.onUpdate.bind(this);
}
render(){
return <div>
<DateRange ref="dateRange" onUpdate={this.onUpdate}/>
<TodayKpi ref="todayKpi" {...this.state}/>
</div>;
}
onUpdate(val){
this.setState({
startDate: val.startDate,
endDate: val.endDate
}, function(){
this.refs.todayKpi.loadTodaysKpi();
});
}
}
Run Code Online (Sandbox Code Playgroud)
我想通过函数onUpdate从DateRange组件中获取一些数据,然后我想在TodayKpi中触发一个从服务器获取数据的函数.现在它只是console.log("AAA");.
子组件:
class TodayKpi extends React.Component {
constructor() {
super();
this.loadTodaysKpi = this.loadTodaysKpi.bind(this);
}
render(){
console.log(this.props.startDate + " "+ this.props.endDate);
return <div className="today-kpi">
</div>;
}
loadTodaysKpi(){
console.log("AAAA");
}
}
Run Code Online (Sandbox Code Playgroud)
我该如何实现呢?
reactjs ×3
call ×1
dom ×1
events ×1
eventtrigger ×1
function ×1
hover ×1
html ×1
javascript ×1
react-jsx ×1