[英]How create a react-native custom view with typescript
I want to create a custom view with react-native-modal in my custom view:我想在我的自定义视图中创建一个带有 react-native-modal 的自定义视图:
type Props = {isVisible: boolean;setVisible: void }
<Modal isVisible={props.visible}......>
<Button title="colse" onPress={()=> props.setVisible}>
Then in my page:然后在我的页面中:
const [isVisible,setVisible]= React.useState(false)
const onClick= () =>{setVisible(!isVisible)}
<MyView visible={isVisible} setVisible={onClick}/>
But when I click the button, nothing happens and there is no error.但是当我单击按钮时,什么也没有发生,也没有错误。
If I change the code like this:如果我像这样更改代码:
const [isVisible,setVisible]= React.useState(true)
the dialog will be shown at first, but cannot close.对话框将首先显示,但无法关闭。
I hope someone can give me an example of how to do that.我希望有人能给我一个如何做到这一点的例子。
Try this way试试这个方法
<Button title="colse" onPress={()=> props.setVisible()}>
const [isVisible,setVisible] = React.useState(false)
const onClick= () => {setVisible(!isVisible)}
<MyView visible={isVisible} setVisible={onClick}/>
Have a try by updating the state based on the previous state .尝试在之前的 state 的基础上更新 state 。
like:喜欢:
const onClick = () => {setVisible(prevState => !prevState)}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.