Javascript 如何从componentDidUpdate()获取值并将其保存,以便以后在显示时使用 导出默认类newroom扩展组件{ 建造师(道具){ 超级(道具) 此.state={ 当前用户:楠, 房间:楠 } } 静态getDerivedStateFromProps(下一步,上一步){ //console.log(prevState) const{currentUser}=nextrops 返回{currentUser} } componentDidUpdate(){ const{…currentUser}=this.state.currentUser currentUser.getJoineableRooms() .然后((房间)=>{ //这个房间有一个id和一个我想存储的名称,这样我可以显示在h3标签中 }) .catch(err=>console.log(err)) } render(){ 返回( {} ) } }

Javascript 如何从componentDidUpdate()获取值并将其保存,以便以后在显示时使用 导出默认类newroom扩展组件{ 建造师(道具){ 超级(道具) 此.state={ 当前用户:楠, 房间:楠 } } 静态getDerivedStateFromProps(下一步,上一步){ //console.log(prevState) const{currentUser}=nextrops 返回{currentUser} } componentDidUpdate(){ const{…currentUser}=this.state.currentUser currentUser.getJoineableRooms() .然后((房间)=>{ //这个房间有一个id和一个我想存储的名称,这样我可以显示在h3标签中 }) .catch(err=>console.log(err)) } render(){ 返回( {} ) } },javascript,reactjs,Javascript,Reactjs,getDerivedStateFromProps()方法返回currentUser对象并设置状态, 当currentuser更新componentDidUpdate方法时,会激发并获取rooms对象,但我不知道如何存储rooms.id和rooms.name,以便以后可以在h3标记中显示。如果我在componentDidUpdate()中使用setState每次都会更新状态并再次激发该方法。您应该避免使用getDerivedStateFromProps,并在componentDidMount方法中

getDerivedStateFromProps()方法返回currentUser对象并设置状态,
当currentuser更新componentDidUpdate方法时,会激发并获取rooms对象,但我不知道如何存储rooms.id和rooms.name,以便以后可以在h3标记中显示。如果我在componentDidUpdate()中使用setState每次都会更新状态并再次激发该方法。

您应该避免使用
getDerivedStateFromProps
,并在componentDidMount方法中进行获取(首次装入时):

导出默认类newroom扩展组件{
建造师(道具){
超级(道具)
此.state={
房间:楠
}
}
componentDidMount(){
const{currentUser}=this.props
currentUser.getJoineableRooms()
.然后((房间)=>{
this.setState({rooms})
})
.catch(err=>console.log(err))
}
render(){
返回(
{rooms.map(room=>{room.id}}
)
}

在调用getJoinableRooms之前,将当前道具与以前的道具进行比较

export default class newroom extends Component {

  constructor(props) {
    super(props)
    this.state = {
       rooms:NaN
    }
  }

  componentDidMount(){
    const { currentUser } = this.props

    currentUser.getJoinableRooms()
            .then((rooms)=>{
              this.setState({rooms})
            })
            .catch(err=>console.log(err))

  }

  render() {

    return (
      <div className="new-room">
        {rooms.map(room => <h3 key={room.id}>{room.id}</h3>)}
      </div>
    )
  }
export default class newroom extends Component {

  constructor(props) {
    super(props)
    this.state = {
       rooms:NaN
    }
  }

  componentDidMount(){
    const { currentUser } = this.props

    currentUser.getJoinableRooms()
            .then((rooms)=>{
              this.setState({rooms})
            })
            .catch(err=>console.log(err))

  }

  render() {

    return (
      <div className="new-room">
        {rooms.map(room => <h3 key={room.id}>{room.id}</h3>)}
      </div>
    )
  }
componentDidUpdate(prevProps, prevState){
    if(prevProps.currentUser.id !== this.props.currentUser.id){
         this.props.currentUser.getJoinableRooms()
            .then((rooms)=>{
                //set state here
         })
        .catch(err=>console.log(err))

     }


}