简体   繁体   English

我无法在子组件中更新 state

[英]I can't update state in child component

i wanna change state value in child component but i getting always 'this.setState is not a function ' error我想更改子组件中的 state 值,但我总是收到“this.setState is not a function”错误

Parent Component父组件

` `

export default class Todo extends Component {
   constructor(props){
    super(props)
    this.state = { 
      propBottomMenu : false
    }
    this.bottomRef = React.createRef()
  };
  checkClose(){
    this.setState({propBottomMenu : false})
  }
  render() {
    return (
      <>
     // other codes 
     <TouchableOpacity 
         onPress={() => this.setState({propBottomMenu : false})}
         style={styles.addTask}
          >
            <FontAwesomeIcon icon={ faPlus } size={25}  color={'#fff'} />
     </TouchableOpacity>
      {this.state.propBottomMenu ? 
         <BottomMenu bSheetRef={this.bottomRef} checkClose={this.checkClose} style=                       {styles.bottomMenu} /> 
        : null}
      </> 
    )
  }
}

` `

Child Component:子组件:

` `

export default class BottomMenu extends Component {
    constructor(props){
        super(props)
        this.bottomRef = this.props.bSheetRef
     }
     render() {
       return (
     <>
        <BottomSheet 
            ref={this.bottomRef} 
            snapPoints={[ '40%', '60%', '90%']} 
            index={1}
            enablePanDownToClose={true}
            onChange={(index)=>{ index < 0 && this.props.checkClose() }}
           >
            // other codes 
          </BottomSheet>
        </>
      )
     }
   }
  })

` `

checkClose() function working but i can't update state checkClose() function 工作但我无法更新 state

Error: this.setState is not a function错误:this.setState 不是 function

Solution: For the function to work we must use bind or call it as arrow function解决方案:要使 function 正常工作,我们必须使用绑定或将其称为箭头 function

constructor(props){
super(props)
this.checkClose = this.checkClose.bind(this); // this line 
this.state = { 
  propBottomMenu : false
}
this.bottomRef = React.createRef()
};

Jsx: Jsx:

<BottomMenu bSheetRef={this.bottomRef} checkClose={this.checkClose} style={styles.bottomMenu} />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从子组件更新我的父 state? - How can I update my parent state from child component? 我无法将使用 useState 更改的 state 发送到子组件 - I can't send the state I changed with useState to the child component 子组件无法通过父组件中的 state 更新正确更新 - Child component doesn't update correctly with state update in parent component 如何更新子组件的 state? - How do I update the state of a child component? 当提供程序组件父状态更改时,为什么无法更新子组件中的值(使用react上下文)? - Why can't update a value in a child component (with react context) when a provider component parent state change? 为什么我无法更新我的反应组件的状态? - Why I can't get update the state of my react component? 当它作为道具传递时,如何将状态从父组件更新到子组件? - How can I update the state from parent component to child component when its passed as a prop? 无法从子组件访问状态 - Can't access state from child component 当子功能组件重定向时,无法对父功能组件中的未安装组件警告执行 React 状态更新 - Can't perform a React state update on an unmounted component warning in Parent functional component when Child functional component redirects 无法更新此React组件的状态 - Can't update the state on this React Component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM