简体   繁体   English

如何使用 react native 单击由函数呈现的按钮?

[英]How do I click on a button rendered with the function using react native?

[Here is My Code ( https://hizliresim.com/gPmJDO ) [这是我的代码( https://hizliresim.com/gPmJDO

Here is My Error ( https://hizliresim.com/dLYAaD )这是我的错误( https://hizliresim.com/dLYAaD

Without binding your function to a component instance, make them as arrow functions无需将您的函数绑定到组件实例,将它们设为箭头函数

handleClick = () => {
  alert("test");
};

renderGridItem = ({ item }) => (
  <TouchableOpacity onPress={this.handleClick}>
      ...
  </TouchableOpacity>
);

Hope this helps you.希望这对你有帮助。 Feel free for doubts.如有疑问,请放心。

Make a handler function as arrow function.将处理函数作为箭头函数。

  handleClick = () => {
      alert("Test Alert");
    }

An then call it然后调用它

renderGridItem = ({ item }) => (
  <TouchableOpacity onPress={()=>this.handleClick()}>
      ....
      ...
  </TouchableOpacity>
)

You don.t need to bind if you use handler function as an arrow function.如果使用处理函数作为箭头函数,则不需要绑定。

暂无
暂无

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

相关问题 如何在React中渲染的按钮单击时关闭? - How do I close upon button click of a rendered component in react? 仅在单击单个 function 按钮后,如何在本机反应中运行 function? - How do I run a function in react native only after button click on a single function? 如何在React Native中将渲染项目设置为状态? - How do I set a rendered item as a state in React Native? 如何将 React 组件呈现的按钮绑定到该组件中的函数? - How can I bind a button rendered by a React component to a function in that component? 如何在按钮单击时调用函数以侦听 React Hooks 中的另一次鼠标单击? - How do I call a function on button click to listen for another mouse click in React Hooks? 当我单击反应原生的按钮时如何显示文本? - How to show text when I click on button in react native? 当我单击反应原生的按钮时如何显示或切换文本? - How to show or toggle text when I click on button in react native? 我正在尝试使用 React Native 和 Javascript 将 FlatList 制作成按钮,但我不知道该怎么做 - I'm trying to make a FlatList into a Button using React Native and Javascript, and I have no clue how to do it 我如何在按钮单击调用的函数中在 if/else 条件下使用反应路由器 - how do i use react router in if/else condition in a function called by button click 当我使用 React 单击按钮时,如何从 select 中选择一个选项? - How can I do to choose an option from a select when I click on a button using React?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM