类型'Readonly <{}>'。ts上不存在属性'vis'

时间:2020-09-24 22:39:42

标签: reactjs react-native state tsx

技术:React-Native

DesiredResult :我希望this.state.vis是可解释的(作为布尔值)。应该将其初始化为true,但是,在按钮上单击状态必须设置为false,从而使可见性为假。

错误:位于渲染条件下的if条件中。这行代码无法识别this.state.vis(仅this.props.vis

class ModalSectionCard extends React.Component <{vis:boolean,cardText:string}>{
  
  constructor(props:any){
    super(props);
    this.state ={
      vis: this.props.vis
    };
  }

  
  render(){
    if (this.state.vis){
    return(
      <TouchableOpacity onPress={()=> this.setState({vis:false})}>
        <View style={{backgroundColor:"#333366"}}>
          <ModalSecCard  >
            <Text style={{color:"#fff"}}>{this.props.cardText}</Text>
          </ModalSecCard>
        </View>
      </TouchableOpacity>
    )
    } else{
      return null
    }
  }
}

1 个答案:

答案 0 :(得分:0)

解决了!将组件更改为函数后,使用let [visOpen, setVisOpen] = React.useState(true);onPress={()=> setVisOpen(false)}

相关来源:https://reactjs.org/docs/hooks-state.html