【问题标题】:Property 'vis' does not exist on type 'Readonly<{}>'.ts'Readonly<{}>'.ts 类型上不存在属性 'vis'
【发布时间】:2021-01-11 06:32:54
【问题描述】:

技术:React-Native

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

Bug:位于渲染下的if-condition。这行代码无法识别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
    }
  }
}

【问题讨论】:

    标签: reactjs react-native state tsx


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2018-10-11
      • 2022-10-16
      • 2020-11-07
      • 2020-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-23
      • 2019-12-11
      相关资源
      最近更新 更多