class App extends react.Component {
construstor(){
super()
this.state = {
message: "Hello",
}
}
render(){
const { message } = this.state
return (
{message}
)
}
}
const root = ReactDOM.createRoot(document.querySelector("#root"));
root.render( )
class App extends react.Component {
constructor(){
super()
this.state = {
imgURL: "http://www.img.com"
}
}
render(){
const { imgURL} = this.state
return (
)
}
}
const root = ReactDOM.createRoot(document.querySelector("#root"));
root.render( )
class App extends react.Component {
constructor(){
super()
this.state = {
isActive: true
}
}
render(){
const { isActive} = this.state
// 1. 字符串拼接
const className = `abc cba ${ isActive ? 'active' : '' }`
// 2. 将所有的class 放到数组中
const classList = ["abc", "cba"]
if(isActive) classList.push("active")
// 3. 第三方库 classnames
return (
哈哈哈
哈哈哈
)
}
}
const root = ReactDOM.createRoot(document.querySelector("#root"));
root.render( )
class App extends react.Component {
constructor(){
super()
this.state = {
objStyle: {color: "red", fontSize: "30px"}
}
}
render(){
const { objStyle } = this.state
return (
哈哈哈
哈哈哈
)
}
}
const root = ReactDOM.createRoot(document.querySelector("#root"));
root.render( )
class App extends react.Component {
constructor(){
super()
this.state = {
message: "Hello"
}
}
btnClick() {
con.log("btnClick")
}
render(){
const { message } = this.state
return (
)
}
}
class App extends react.Component {
constructor(){
super()
this.state = {
counter: 100,
message: "Hello"
}
// 在这里写也可以, 在render 函数中直接通过this.btn1Click 调用即可
// this.btn1Click = this.btn1Click.bind(this)
}
btn1Click() {
con.log("btn1Click", this)
this.setState({
counter: this.state.counter + 1
})
}
btn2Click = () => {
con.log("btn2Click", this)
this.setState({
counter: this.state.counter + 2
})
}
// *
btn3Click() {
con.log("btn3Click", this)
this.setState({
counter: this.state.counter + 3
})
}
render(){
const { counter, message } = this.state
return (
当前计数: {this.state.counter}
)
}
}
class App extends react.Component {
constructor(){
super()
this.state = {
counter: 100,
message: "Hello"
}
}
btnClick(event) {
console.log("btnClick", event, this)
}
render(){
const { counter, message } = this.state
return (
当前计数: {this.state.counter}
)
}
}
class App extends react.Component {
constructor(){
super()
this.state = {
counter: 100,
message: "Hello"
}
}
btnClick(event, name, age) {
console.log("btnClick", event, this)
console.log("btnClick", name, age)
}
render(){
const { counter, message } = this.state
return (
当前计数: {this.state.counter}
)
}
}
class App extends react.Component {
constructor(){
super()
this.state = {
isReady: true,
friend: {
name: "kobe",
desc: "ball"
}
}
}
render(){
const { isReady, friend } = this.state
let showElement = null
if(isReady) {
showElement = 准备
} else {
showElement = 未准备
}
return (
方式一:根据条件给变量赋值不同的内容
{showElement}
方式二:三元运算符
{ isReady ? : 准备就绪
}
方式三:逻辑与运算符 &&
使用场景:当某个值有可能为undefined或null 时,可以使用&& 条件判断
例如:服务器中数据还未取回时为null,有了数据后再进行展示
{ friend && { friend.name + " " + friend.desc } }
)
}
}
class App extends react.Component {
constructor(){
super()
this.state = {
students: [
{ id: 1, name:"why", score: 99 },
{ id: 2, name:"kobe", score: 98 },
{ id: 3, name:"james", score: 97 },
{ id: 4, name:"curry", score: 96 }
]
}
}
render(){
const { students } = this.state
return (
{
students.filter(item => item.score > 97).slice(0, 2).map(item => {
return (
学号: {item.id}
姓名: {item.name}
分数: {item.score}
)
})
}
)
}
}
注意:项目名称不能包含大写字母
constructor → render → (update DOM and refs )→ componentDidMount()
( new props、setState()、forceUpdate() )
render → (update DOM and refs )→ componentDidUpdate()
componentWillUnmount()