Chr*_*cke 1 javascript state reactjs
我有一个像这样的小组件(下面的代码被简化为所需的部分),在更新状态时表现得非常奇怪。
class Componenent extends React.Component {
constructor(props) {
super(props);
this.state = {showStuff: false};
}
render() {
return(
//Markup
{this.state.showStuff && (
<button onClick={() => this.setState({showStuff: false})} />
)}
// More Markup
);
}
}
Run Code Online (Sandbox Code Playgroud)
状态会在组件的其他地方更新,所以 prop 是true在单击按钮时。单击也会触发该setState函数(执行回调),但状态不会更新。
test: true状态添加另一个道具并将该属性更改false为单击按钮时也会触发showStuff道具更改为 false。所以当我进行奇怪的黑客攻击时它会起作用。class ElementAdd extends React.Component {
constructor(props) {
super(props);
this.defaultState = {
showElementWheel: false,
test: true
};
this.state = this.defaultState;
}
handleAddCardClick() {
if (this.props.onCardAdd) {
this.props.onCardAdd({
type: ElementTypes.card,
position: this.props.index
});
}
}
handleAddKnowledgeClick() {
if (this.props.onCardAdd) {
this.props.onCardAdd({
type: ElementTypes.knowledge,
position: this.props.index
});
}
}
handleTabPress(e) {
if (e.key === 'Tab') {
e.preventDefault();
let target = null;
if (e.shiftKey) {
if (e.target.previousSibling) {
target = e.target.previousSibling;
} else {
target = e.target.nextSibling;
}
} else {
if (e.target.nextSibling) {
target = e.target.nextSibling;
} else {
target = e.target.previousSibling;
}
}
target.focus();
}
}
hideElementWheel() {
// This is somehow the only option to trigger the showElementWheel
this.setState({ test: false });
}
render() {
return (
<div
className="element-add"
style={{ opacity: this.props.invisible ? 0 : 1 }}
onClick={() => this.setState(prevSate => ({ showElementWheel: !prevSate.showElementWheel }))}
>
<PlusIcon className="element-add__icon" />
{this.state.showElementWheel && (
<React.Fragment>
<div className="element-add__wheel">
<button
autoFocus
className="element-add__circle"
onClick={this.handleAddCardClick.bind(this)}
onKeyDown={this.handleTabPress.bind(this)}
title="New element"
>
<ViewModuleIcon className="element-add__element-icon" />
</button>
<button
className="element-add__circle"
onClick={this.handleAddKnowledgeClick.bind(this)}
onKeyDown={this.handleTabPress.bind(this)}
title="New knowledge-element"
>
<FileIcon className="element-add__element-icon" />
</button>
</div>
<div
className="element-add__close-layer"
onClick={() => {
this.hideElementWheel();
}}
/>
</React.Fragment>
)}
</div>
);
}
}
Run Code Online (Sandbox Code Playgroud)
通过编写,onClick={this.setState({showStuff: false})}您实际上setState是在按钮呈现后立即调用。
你想给一个函数引用onClick,而不是在渲染时立即调用它。
<button onClick={() => this.setState({showStuff: false})} />
Run Code Online (Sandbox Code Playgroud)
如果您的按钮位于另一个带有单击侦听器的元素内,而您不想在同一次单击时运行,则必须确保单击事件不会传播到父级。
<button
onClick={(event) => {
event.stopPropagation();
this.setState({showStuff: false});
}}
/>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1842 次 |
| 最近记录: |