标签:exp 严格 int 数据流 use 监听 index 需要 last
1.npm install -g create-react-app 安装脚手架
2.create-react-app pros-name 初始化
3.react的api比较少,基本一次,之后不用看文档了,核心js功力
import React from 'react'
import ReactDOM from 'react-dom'
import App from './App'
ReactDOM.render(<App/>,document.querySelector('#root'))
import React from 'react'
function App(){
return (
<h1>HelloWorld</h1>
)
}
export default App
ReactDOM.render(<App/>,document.querySelector('#root'))
类似js和html的混合,我们称JSX,核心是js实现
function App(){
const name = 'steve yu'
return (
<div>
<h1>{name}</h1>
</div>
)
}
function formatName(user){
return user.firstName+' '+user.lastName
}
function App(){
const user = {firstName:'steve',lastName:'yu'}
return (
<div>
<h1>{formatName(user)}</h1>
</div>
)
}
//函数类型的组件
export function Welcome1(){
return (
<div>
welcome1
</div>
)
}
//基于类的组件
export class Welcome2 extends React.Component{
render(){
return <div>welcome2</div>
}
}
function App() {
return (
<div>
<Welcome1></Welcome1>
<Welcome2></Welcome2>
</div>
);
}
export default class Clock extends Component{
state = {
date : new Date()
}
componentDidMount(){
this.timer = setInterval(()=>{
this.setState({
date: new Date()
})
},1000)
}
componentWillUnmount(){
clearInterval(this.timer)
}
render(){
return (
<div>
{this.state.date.toLocaleTimeString()}
</div>
)
}
}
export default class Cart extends Component{
constructor(props){
super(props)
this.state = {
goods : [
{id:1,text:'milk'},
{id:2,text:'banana'},
{id:3,text:'apple'}
]
}
}
render(){
return(
<div>
{/* 条件渲染 */}
{this.props.title && <h1>{this.props.title}</h1>}
{/* 列表渲染 */}
<ul>
{this.state.goods.map(good=>
<li key={good.id}>{good.text}</li>
)}
</ul>
</div>
)
}
}
React中使用onClick类似写法来监听事件,注意this绑定问题,react里严格遵循单项数据流,没有数据的双向绑定,所以输入框要设置value和onChange
标签:exp 严格 int 数据流 use 监听 index 需要 last
原文地址:https://www.cnblogs.com/littlepage/p/12099980.html