简体   繁体   English

不使用react-native-web为ReactJS创建“ TouchableOpacity”

[英]create “TouchableOpacity” for ReactJS without using react-native-web

I'd like to create a wrapper component that will behave like react- native s <TouchableOpacity/> without using react-native-web . 我想创建一个包装器组件,其行为类似于react- native<TouchableOpacity/> react- native <TouchableOpacity/>而无需使用react-native-web

I'm thinking something that manages state and using inline styles that transitions from .5 to 1 for opacity with a setInterval or should it be a css transition. 我正在考虑管理状态并使用内联样式(使用setInterval.5转换为1的不透明性),或者应该是css转换。

Is there anything already out there? 已经有东西了吗?

What's the best approach for this? 最好的方法是什么?

You can achieve this affect with css transitions and using state to track when the user is clicking the button: 您可以通过CSS过渡和使用状态来跟踪用户单击按钮的时间来实现此效果:

 class App extends React.Component { state = { touched: false } toggleTouched = () => { this.setState( prevState => ({ touched: !prevState.touched })); } handleMouseUp = () => { // Handle smooth animation when clicking without holding setTimeout( () => { this.setState({ touched: false }); }, 150); } render () { const { touched } = this.state; const className = touched ? 'btn touched' : 'btn'; return ( <button className={className} onMouseDown={this.toggleTouched} onMouseUp={this.handleMouseUp} > Touch Here </button> ) } } ReactDOM.render(<App/>, document.getElementById('root')); 
 .btn { background: #ededed; padding: 10px 15px; border: none; outline: none; cursor: pointer; font-size: 15px; opacity: 1; transition: opacity 300ms ease; } .touched { opacity: 0.5; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script> <div id="root"></div> 

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

相关问题 react-native-web 如何配置 webpack - react-native-web how to configure webpack React-Native-Web 的视频支持 - Video suport for React-Native-Web 样式组件:使用 react-native 和 react-native-web 悬停 - styled components :hover with react-native and react-native-web 'ActionSheetIOS' 不是从'react-native-web/dist/index' 导出的 - 'ActionSheetIOS' is not exported from 'react-native-web/dist/index' 有条件地将电子导入 react-native-web 应用程序 - Importing electron conditionally into react-native-web app 如何在 react-native-web 中使用情感? - How can I use emotion with react-native-web? 将React-Navigation与react-native-web一起使用会引发错误,因为它无法导入与Web相关的模块 - Using React-Navigation with react-native-web throws me error because it cannot import web related module 在 react-native-web monorepo 架构中尝试导入错误 - Attempted import errors in react-native-web monorepo architecture 尝试导入错误:当编译react-native到react-native-web时,不会从&#39;./apiFetcher&#39;导出&#39;ApiGet&#39; - Attempted import error: 'ApiGet' is not exported from './apiFetcher' when compile react-native to react-native-web React-Native - 如何为 TouchableOpacity 组件创建禁用样式? - React-Native - How to create a disabled style for the TouchableOpacity component?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM