简体   繁体   English

如何使用 typescript 创建 react-native 自定义视图

[英]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.

相关问题 如何扩展本机组件打字稿 - How to extend react-native component typescript 如何使用 Yarn 和 Typescript 设置 React-Native - How to setup React-Native with Yarn and Typescript 如何为 react-native 本机 ui 组件创建打字稿类型定义? - How do I create typescript type definitions for react-native native ui component? 如何在函数内的 React-Native Typescript 中动态创建和填充数组? - How to dynamically create and populate an array in React-Native Typescript within a function? 带打字稿的React-Native导航 - React-Native Navigation with typescript React-native:如何在React-native中将jsx打字稿.tsx文件使用(和翻译)? - React-native: How to use (and translate) typescript .tsx files with jsx in React-native? 如何在打字稿本机项目中导入降价文件? - How to import a markdown file in a typescript react-native project? 如何在 TypeScript react-native 中使用固有属性作为类型 - How to use an Intrinsic attributes as types in TypeScript react-native 如何通过 typescript 描述 React-Native(-Web) 中的 ScrollEvent 类型? - How to describe the ScrollEvent type in React-Native(-Web) via typescript? 如何通过 Typescript 为 Flow 导入 React-Native 类型? - How to import React-Native types via Typescript for Flow?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM