
内联样式是一种强大的工具,它使开发人员能够在组件级别动态设置样式,而不必依赖外部CSS文件。
内联样式的优点:
state 中的状态内联样式的缺点:
import React from 'react';
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
color: 'red'
}
}
render() {
return (
<div>
<p style={{fontSize: '50px', color: this.state.color}}>我是段落1</p>
<p style={{fontSize: '50px', color: 'green'}}>我是段落2</p>
<button onClick={() => {
this.btnClick()
}}>按钮
</button>
</div>
);
}
btnClick() {
this.setState({
color: 'blue'
})
}
}
export default App;

官方文档:https://zh-hans.reactjs.org/docs/reconciliation.html#the-diffing-algorithm

App.js:
import React from 'react';
class Home extends React.Component {
constructor(props) {
super(props);
this.state = {
heroList: ['鲁班', '虞姬', '黄忠']
}
}
render() {
return (
<div>
<ul>{
this.state.heroList.map(name => {
return <li key={name}>{name}</li>
})
}</ul>
<button onClick={() => {
this.btnClick()
}}>按钮
</button>
</div>
)
}
btnClick() {
this.setState({
heroList: ['阿珂', ...this.state.heroList]
})
}
}
class App extends React.Component {
render() {
return (
<div>
<Home/>
</div>
)
}
}
export default App;
本期结束咱们下次再见👋~
🌊 关注我不迷路,如果本篇文章对你有所帮助,或者你有什么疑问,欢迎在评论区留言,我一般看到都会回复的。大家点赞支持一下哟~ 💗

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。