state
是組件保存,控制,修改本身的可變狀態。state
能夠經過 props 來初始化本身的狀態 stateprops
主要做用是讓使用該組件的父組件能夠傳入參數來配置該組件若是你以爲仍是搞不清
state
和props
的使用場景,那麼請記住一個簡單的規則:儘可能少地用state
,儘可能多地用props
。javascript
state
的組件叫做無狀態組件(stateless component)state
的組件叫做有狀態組件(stateful component)react.js 鼓勵無狀態組件在 0.14 版本引入函數式組件-----一種不能使用
state
組件html
class HelloWorld extends Component {
constructor() {
super()
}
sayHi () {
alert('Hello World')
}
render () {
return (
<div onClick={this.sayHi.bind(this)}>Hello World</div>
)
}
}
複製代碼
const HelloWorld = (props) => {
const sayHi = (event) => alert('Hello World')
return (
<div onClick={sayHi}>Hello World</div>
)
}
複製代碼
函數式組件就是一種只能接受
props
和 提供 render 方法的類組件前端
state
留在上層數據bad one java
good one react
better one框架
一、book : react 小書less
二、react 官網函數