export class Profile extends Component {
render() {
console.log(this.context);
return (
Profile
)
}
}
当 render 被调用时,它会检查 this.props 和 this.state 的变化并返回以下类型之一:
函数组件是使用function来进行定义的函数,只是这个函数会返回和类组件中render函数返回一样的内容。
函数组件有自己的特点:
React内部为了告诉我们当前处于哪些阶段,会对我们组件内部实现的某些函数进行回调,这些函数就是生命周期函数:
父组件:
import React from "react"
// import HelloWorld from "./Components/HelloWorld"
import TabControl from './Components/父子组件通信案例'
// import NavBar from './Components/实现插槽一'
// import NavBar from './Components/实现插槽二'
// import TabControl from './Components/作用域插槽'
// import AppHome from "./Components/非父子组件通信-context/AppHome";
class App extends React.Component {
constructor() {
super()
console.log("hello react");
this.state = {
message: "hello react",
isShow: true,
titles: ['流行', '新款', '精选'],
curIndex: 0
}
}
changeText() {
this.setState({
message: 'hello hgf'
})
}
switchText() {
this.setState({
isShow: !this.state.isShow
})
}
switchTab(curIndex){
this.setState({curIndex})
}
render() {
const { titles, curIndex } = this.state
return (
this.switchTab(curIndex)} />
{/* {titles[curIndex]}
*/}
{/* {message}
*/}
{/* */}
{/* {isShow && } */}
{/* */}
{/* 插槽实现1 */}
{/*
中间部分
*/}
{/* 插槽实现二 */}
{/* 左按钮} centerSlot={插槽实现二
} rightSlot={} /> */}
{/* 作用域插槽 */}
{/* } changeTab={(curIndex) => this.switchTab(curIndex)} />
{titles[curIndex]}
*/}
{/* 非父子组件通信 */}
{/* */}
)
}
componentDidMount() {
console.log("component Mount")
}
componentDidUpdate() {
console.log("component Update");
}
}
export default App
子组件:
import React, { Component } from 'react'
import "./tab-control.css"
export class index extends Component {
constructor() {
super()
this.state = {
curIndex: 0
}
}
changeCurIndex(index) {
this.setState({curIndex: index})
this.props.changeTab(index)
}
render() {
const {titles} = this.props
const {curIndex} = this.state
return (
{
titles.map((item, index) => {
return (
this.changeCurIndex(index)}>
{item}
)
})
}
)
}
}
export default index
React对于这种需要插槽的情况非常灵活,有两种方案可以实现:
import React, { Component } from 'react'
import "./style.css"
export class index extends Component {
render() {
const {children} = this.props
return (
{children[0]}
{children[1]}
{children[2]}
)
}
}
// index.propTypes = {
// children: PropTypes.element
// }
export default index
import React, { Component } from 'react'
import "./style.css"
export class index extends Component {
render() {
const {leftSlot, centerSlot, rightSlot} = this.props
return (
{leftSlot}
{centerSlot}
{rightSlot}
)
}
}
// index.propTypes = {
// children: PropTypes.element
// }
export default index
对于有一些场景:比如一些数据需要在多个组件中进行共享(地区偏好、UI主题、用户登录状态、用户信息等)
创建一个需要共享的Context对象
import React from "react";
const ThemeContext = React.createContext({nickname: 'curry', work: '前端'})
export default ThemeContext
import React from "react";
const UserContext = React.createContext()
export default UserContext
import React, { Component } from 'react'
import ThemeContext from './context/theme-context'
import UserContext from './context/user-context'
import Home from './context/Home'
import Profile from './context/Profile'
export class AppHome extends Component {
render() {
return (
App
)
}
}
export default AppHome
import React, { Component } from 'react'
import ThemeContext from './theme-context'
export class Profile extends Component {
render() {
console.log(this.context);
return (
Profile
)
}
}
Profile.contextType = ThemeContext
export default Profile
import React, { Component } from 'react'
import ThemeContext from './theme-context';
import UserContext from './user-context';
export class HomeInfo extends Component {
render() {
console.log(this.context);
return (
HomeInfo
{
value => {
return InfoUser: {value.age}
}
}
)
}
}
HomeInfo.contextType = ThemeContext
export default HomeInfo