gpt4 book ai didi

javascript - React 和无状态组件

转载 作者:行者123 更新时间:2023-12-03 06:20:56 25 4
gpt4 key购买 nike

我看到了这条来自React Stateless Functional Components: Nine Wins You Might Have Overlooked的评论:

You should really avoid having function allocations inside statelless components. Just keep them outisde the scope and pass the props, it’s a very big performance difference.

我想知道,这是否是我的组件:

const Text = (props) =>  <p>{props.children}</p>;

// ReactDOM is part of the introduction of React 0.14
ReactDOM.render(
<Text>Hello World</Text>,
document.querySelector('#root')
);

如何访问该文件之外的函数?我应该使用哪个属性?

最佳答案

只需将它们作为 Prop 传递即可。

function clickParagraph() {
console.log('clicked!')
}

const Text = (props) =>
<p onClick={props.onClick}>{props.children}</p>;


ReactDOM.render(
<Text onClick={clickParagraph}>Hello World</Text>,
document.querySelector('#root')
);

关于javascript - React 和无状态组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38877643/

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