react中的state

import React, {Component} from 'react'

class State extends Component{
    constructor(props){
        super(props)
        this.state = {
            name:'SB',
            testName:'sss',
            intervalName:''
        }
    }
    componentDidMount () {
        //修改state中的值的方法1
        this.setState({name:'6666'})
        let intervalName=  setTimeout(()=>{
            //修改state中的值的方法2
            this.setState((prevState, props) => ({
                name: prevState.name + props.name
            }));
        },1000)
        this.setState({intervalName:intervalName})
    }
    componentWillUnmount () {
        clearTimeout(this.state.intervalName)
    }
    render(){
        return(
            
现在state中name的值{this.state.name}{this.state.testName}
) } } export default State

你可能感兴趣的:(react中的state)