Kar*_*rol 10 javascript reactjs
我正在做出反应,基本上我想用工具提示制作一个按钮,现在我正在制作工具提示.我正在更改css显示属性,以便在鼠标进入和离开期间使其可见或不可见.但是有一个错误,我不知道该怎么做......
这是我的代码:
import React from 'react';
import ReactDOM from 'react-dom';
import Style from 'style-it';
var Ink = require('react-ink');
import FontIcon from '../FontIcon/FontIcon';
var IconButton = React.createClass({
getInitialState() {
return {
iconStyle: "",
style: "",
cursorPos: {},
};
},
extend(obj, src) {
Object.keys(src).forEach(function(key) { obj[key] = src[key]; });
return obj;
},
Tooltip(props) {
var style = {};
if (this.tooltipDisplay) {
style.display = "block";
} else if (!this.tooltipDisplay) {
style.display = "none";
};
return <div className="tooltip" style={style}>{_props.tooltip}</div>;
},
showTooltip(){
this.tooltipDisplay = true;
},
removeTooltip(){
this.tooltipDisplay = false;
},
render() {
var _props = this.props,
tooltip = this.Tooltip,
opts,
tooltipDisplay = false,
disabled = false,
rippleOpacity,
outterStyleMy = {
border: "none",
outline: "none",
padding: "8px 10px",
"background-color": "red",
"border-radius": 100 + "%",
cursor: "pointer",
},
iconStyleMy = {
"font-size": 12 + "px",
"text-decoration": "none",
"text-align": "center",
display: 'flex',
'justify-content': 'center',
'align-items': 'center',
},
rippleStyle = {
color: "rgba(0,0,0,0.5)",
};
if (_props.disabled || _props.disableTouchRipple) {
rippleStyle.opacity = 0;
};
this.setState({
iconStyle: _props.iconStyle
});
this.setState({
style: _props.style
});
if (_props.disabled) {
disabled = true;
};
if (this.state.labelStyle) {
iconStyleMy = this.state.iconStyle;
};
if (this.state.style) {
outterStyleMy = this.state.style;
};
if (_props.href) {
opts.href = _props.href;
};
var buttonStyle = this.extend(outterStyleMy, iconStyleMy);
return(
<Style>
{`
.IconButton{
position: relative;
}
.IconButton:disabled{
color: ${_props.disabledColor};
}
.btnhref{
text-decoration: none;
}
`}
<a {...opts} className="btnhref" >
<tooltip text={this.props.tooltip} position={this.options} />
<button ref="button" className={"IconButton" + _props.className} disabled={disabled} style={buttonStyle}
onMouseEnter={this.showTooltip} onMouseLeave={this.removeTooltip} >
<Ink background={true} style={rippleStyle} opacity={rippleOpacity} />
<FontIcon className={_props.iconClassName}/>
</button>
</a>
</Style>
);
}
});
ReactDOM.render(
<IconButton href="" className="" iconStyle="" style="" iconClassName="face" disabled="" disableTouchRipple="" tooltip="aaaaa" />,
document.getElementById('app')
);
Run Code Online (Sandbox Code Playgroud)
在控制台中我收到此错误:
Uncaught RangeError: Maximum call stack size exceeded
at defineRefPropWarningGetter (App.js:1053)
at Object.ReactElement.createElement (App.js:1220)
at Object.createElement (App.js:3329)
at Constructor.render (App.js:43403)
at App.js:15952
at measureLifeCyclePerf (App.js:15233)
at ReactCompositeComponentWrapper._renderValidatedComponentWithoutOwnerOrContext (App.js:15951)
at ReactCompositeComponentWrapper._renderValidatedComponent (App.js:15978)
at ReactCompositeComponentWrapper._updateRenderedComponent (App.js:15902)
at ReactCompositeComponentWrapper._performComponentUpdate (App.js:15880)
Run Code Online (Sandbox Code Playgroud)
我找不出有什么问题.我知道这可能是调用一个函数,而函数又调用另一个函数.但是在我的代码中我看不到这样的东西,我不确定它是否都是关于它的.感谢帮助 :)
Rya*_*ale 31
每当你看到setState从render函数内部调用的代码时,它应该唤起与人类蜈蚣电影相同的情感类型.状态更改应该仅在更改后发生:用户单击按钮,浏览器窗口调整大小,拍摄照片等.
这是问题代码:
render () {
...
this.setState({
iconStyle: _props.iconStyle
});
this.setState({
style: _props.style
});
...
}
Run Code Online (Sandbox Code Playgroud)
[呕吐在我嘴里]
上面的代码会导致无限循环的排序,因为要调用的setState原因render.由于iconStyle和style是道具,道具无法改变,你应该使用这些道具来建立你的初始状态.
getInitialState() {
return {
iconStyle: this.props.iconStyle,
style: this.props.style,
cursorPos: {},
};
}
Run Code Online (Sandbox Code Playgroud)
稍后,如果有人单击某个按钮并且您希望更改iconStyle,则会创建一个更新您的状态的单击处理程序:
handleClick () {
this.setState({
iconStyle: 'clicked'
});
}
Run Code Online (Sandbox Code Playgroud)
这将导致您的组件被重新呈现,并且将反映新状态.
把你的"状态"想象成一个烹饪的人,我们将拍摄他们烹饪的照片.在初始状态是"蛋破碎:没有,面粉倒:没有,蔬菜切碎:没有 ",你拿这个状态的照片.然后厨师做了一些事 - 打破鸡蛋.国家现在已经改变了,你拍了一张照片.然后她切蔬菜.再次,国家已经改变,你拍了一张照片.
类比中的每张照片代表您的"渲染"功能 - 特定时间点的"状态"快照.如果你每次拍照都面粉倒了,那么我们就不得不拍另一张照片,因为面粉只是倒了.拍摄另一张照片会导致更多的面粉倒入,所以我们不得不拍另一张照片.最后,你会把厨房塞进天花板,带着乳糜泻的噩梦,让房间里的每个人都窒息.您的相机也会耗尽胶片或硬盘空间.
感谢@RyanWheale我注意到了我的错误.
在我的渲染函数中,我返回了一个按钮元素,该元素调用了一个改变某种状态的函数.返回的按钮看起来像这样:
<button onclick={this.edit()} className="button-primary">Edit</button>
Run Code Online (Sandbox Code Playgroud)
我的edit函数改变了一些状态,看起来像这样:
edit: function () {
this.setState({editing: true});
}
Run Code Online (Sandbox Code Playgroud)
所以,我的错误在于我不小心输入了括号this.edit.因此,当渲染按钮元素时,实际上调用了编辑函数并且发生了混乱.现在,当我写作
<button onclick={this.edit} className="button-primary">Edit</button>
代替
<button onclick={this.edit()} className="button-primary">Edit</button>
它完美无缺.我希望我能帮助别人节省宝贵的生命.
干杯:)
| 归档时间: |
|
| 查看次数: |
19537 次 |
| 最近记录: |