gpt4 book ai didi

javascript - 阻止点击事件

转载 作者:太空宇宙 更新时间:2023-11-04 16:25:19 24 4
gpt4 key购买 nike

我有一个包含一组按钮的容器,但让我们将该组件视为单个组件。该组件有一个 onBlockClick,它接收前往何处的 id

我想将 id 转换为 registrationSomeContainer

我的问题是,如何从 onBlockClick 处理其他容器?

let registrationContainer = ({
i18n,
id,
name,
representative,
onBlockClick,
dispatch
}) => {

return (
<div>
<div
className="app_wrapper"
>
<Block
leftIcon="close"
lines={[
"Some",
representative
]}
onBlockClick(id)
/>
</div>
</div>
)

}

最佳答案

性能较差的方法是

const registrationContainer = ({
i18n,
id,
name,
representative,
onBlockClick,
dispatch
}) => {
return (
<div className="app_wrapper">
<Block
leftIcon="close"
lines={[
"Some",
representative
]}
onBlockClick={onBlockClick.bind(null, id)}
/>
</div>
);
}

更优化的代码可能是

const registrationContainer = ({
i18n,
id,
name,
representative,
onBlockClick,
dispatch
}) => {
return (
<div className="app_wrapper">
<Block
id={id} // Look: passing id
leftIcon="close"
lines={[
"Some",
representative
]}
onBlockClick={onBlockClick} // Look: not binding
/>
</div>
);
}

class Block extends Component {
handleClick = (e) => {
this.props.onBlockClick(this.props.id);
};

render() {
<div onClick={this.handleClick}>

</div>
}
}

为什么绑定(bind)方式不好

绑定(bind)onBlockClick={onBlockClick.bind(null, id)}使onBlockClick每次都会获得新的引用,从而导致子组件重新渲染。从这里阅读更多信息 https://daveceddia.com/avoid-bind-when-passing-props/

关于javascript - 阻止点击事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40282765/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com