我开始学习 React Native,并为我的项目创建了一个简单的 Button 组件以在我的项目中重复使用。我根据变量“禁用”动态设置不透明度值,但是,按钮的外观不会随着不透明度变量的值而改变。我四处搜索,但没有找到解释..
任何帮助将不胜感激。
这是我的源代码:
import React from 'react'
import { View, Text, TouchableOpacity, StyleSheet } from 'react-native'
import PropTypes from 'prop-types'
//TODO: arrumar o problema com a opacidade
export default function Button({text, onPress, style, disabled, textStyle}) {
let opacity = disabled === true ? 0.5 : 1
// console.log('opacity', opacity)
return (
<TouchableOpacity onPress={onPress} style={[defaultStyles.button, style, {opacity: opacity}]}
disabled={disabled}>
<Text style={[defaultStyles.text, textStyle]}>{text}</Text>
</TouchableOpacity>
)
}
const defaultStyles = StyleSheet.create({
text: {
color: 'white'
},
button: {
backgroundColor: 'black',
margin: 15,
padding: 15,
borderRadius: 10
},
})
Button.propTypes = {
text: PropTypes.string,
onPress: PropTypes.func,
style: PropTypes.oneOfType([
PropTypes.string,
PropTypes.array,
PropTypes.object
]),
disabled: PropTypes.bool,
textStyle: PropTypes.oneOfType([
PropTypes.string,
PropTypes.array,
PropTypes.object
])
}
编辑: 这是调用按钮的代码
class NewDeck extends Component {
state={
title: null
}
submit = () => {
const { add, goBack } = this.props
let deck = {...this.state}
if(!deck['deckId']){
deck['deckId'] = Date.now()
deck['logs'] = []
}
!deck['cardsId'] && (deck['cardsId'] = [])
add(deck).then(() => {
this.props.navigation.navigate('Deck', {deckId: deck.deckId, title: deck.title})
this.setState({title: null})
}
)
}
render(){
const disabled = this.state.title === null || this.state.title.length === 0
return (
<KeyboardAwareScrollView resetScrollToCoords={{ x: 0, y: 0 }}
contentContainerStyle={styles.container}>
<Text style={textStyles.title2}>Whats the title of your deck?</Text>
<TextInput editable={true} style={[styles.input, textStyles.body]}
placeholder='Type title here'
maxLength={25}
value={this.state.title}
onChangeText={(text) => {
this.setState({title: text})
}}
/>
<Button
onPress={this.submit}
text='Submit'
style={{backgroundColor: colors.pink}}
textStyle={textStyles.body}
disabled={!this.state.title}
/>
</KeyboardAwareScrollView>
)
}
}
如果 newDeck 组件的标题为空或 null,则 disabled 变量为真。当此变量为真时,按钮的不透明度应仅为 0.5。当该值变为 false 时,不透明度再次变为 1。如果我记录组件中的不透明度值,我可以看到它从 0.5 变为 1,但组件的外观没有改变。
最佳答案
不确定它是否是 TouchableOpacity 组件上的错误,但在单击该组件之前,不透明度不会在重新渲染时更新
要解决您的问题,只需将 touchable 的内容包裹在 View 中,然后将 opacity 应用于 View 而不是 touchable
export default function Button({text, onPress, style, disabled, textStyle}) {
const opacity = disabled === true ? 0.5 : 1
// console.log('opacity', opacity)
return (
<TouchableOpacity onPress={onPress} disabled={disabled}
style={[defaultStyles.button, style]}>
<View style={{opacity}}>
<Text style={[defaultStyles.text, textStyle]}>{text}</Text>
</View>
</TouchableOpacity>
)
}
关于javascript - 当组件在 native react 中重新呈现时,动态不透明度不会改变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47979866/
我在使用omniauth/openid时遇到了一些麻烦。在尝试进行身份验证时,我在日志中发现了这一点:OpenID::FetchingError:Errorfetchinghttps://www.google.com/accounts/o8/.well-known/host-meta?hd=profiles.google.com%2Fmy_username:undefinedmethod`io'fornil:NilClass重要的是undefinedmethodio'fornil:NilClass来自openid/fetchers.rb,在下面的代码片段中:moduleNetclass
设置:狂欢ruby1.9.2高线(1.6.13)描述:我已经相当习惯在其他一些项目中使用highline,但已经有几个月没有使用它了。现在,在Ruby1.9.2上全新安装时,它似乎不允许在同一行回答提示。所以以前我会看到类似的东西:require"highline/import"ask"Whatisyourfavoritecolor?"并得到:Whatisyourfavoritecolor?|现在我看到类似的东西:Whatisyourfavoritecolor?|竖线(|)符号是我的终端光标。知道为什么会发生这种变化吗? 最佳答案
我在我的Rails项目中使用Pow和powifygem。现在我尝试升级我的ruby版本(从1.9.3到2.0.0,我使用RVM)当我切换ruby版本、安装所有gem依赖项时,我通过运行railss并访问localhost:3000确保该应用程序正常运行以前,我通过使用pow访问http://my_app.dev来浏览我的应用程序。升级后,由于错误Bundler::RubyVersionMismatch:YourRubyversionis1.9.3,butyourGemfilespecified2.0.0,此url不起作用我尝试过的:重新创建pow应用程序重启pow服务器更新战俘
有没有办法在Ruby中动态创建数组?例如,假设我想遍历用户输入的书籍数组:books=gets.chomp用户输入:"TheGreatGatsby,CrimeandPunishment,Dracula,Fahrenheit451,PrideandPrejudice,SenseandSensibility,Slaughterhouse-Five,TheAdventuresofHuckleberryFinn"我把它变成一个数组:books_array=books.split(",")现在,对于用户输入的每一本书,我想用Ruby创建一个数组。伪代码来做到这一点:x=0books_array.
我想在IRB中浏览文件系统并让提示更改以反射(reflect)当前工作目录,但我不知道如何在每个命令后进行提示更新。最终,我想在日常工作中更多地使用IRB,让bash溜走。我在我的.irbrc中试过这个:require'fileutils'includeFileUtilsIRB.conf[:PROMPT][:CUSTOM]={:PROMPT_N=>"\e[1m:\e[m",:PROMPT_I=>"\e[1m#{pwd}>\e[m",:PROMPT_S=>"FOO",:PROMPT_C=>"\e[1m#{pwd}>\e[m",:RETURN=>""}IRB.conf[:PROMPT_MO
我遇到了一个非常奇怪的问题,我很难解决。在我看来,我有一个与data-remote="true"和data-method="delete"的链接。当我单击该链接时,我可以看到对我的Rails服务器的DELETE请求。返回的JS代码会更改此链接的属性,其中包括href和data-method。再次单击此链接后,我的服务器收到了对新href的请求,但使用的是旧的data-method,即使我已将其从DELETE到POST(它仍然发送一个DELETE请求)。但是,如果我刷新页面,HTML与"new"HTML相同(随返回的JS发生变化),但它实际上发送了正确的请求类型。这就是这个问题令我困惑的
我有可变数量的表格和可变数量的行,我想让它们一个接一个地显示,但如果表格不适合当前页面,请将其放在下一页,然后继续。我已将表格放入事务中,以便我可以回滚然后打印它(如果高度适合当前页面),但我如何获得表格高度?我现在有这段代码pdf.transactiondopdf.table@data,:font_size=>12,:border_style=>:grid,:horizontal_padding=>10,:vertical_padding=>3,:border_width=>2,:position=>:left,:row_colors=>["FFFFFF","DDDDDD"]pdf.
首先,我使用的是rails3.1.3和来自master的carrierwavegithub仓库的分支。我使用after_init钩子(Hook)来确定基于属性的字段页面模型实例并为这些字段定义属性访问器将值存储在序列化哈希中(希望它清楚我是什么谈论)。这是我正在做的事情的精简版:classPage省略mount_uploader命令让我可以访问我想要的属性。但是当我安装uploader时出现错误消息说“nil类的未定义新方法”我在源代码中读到有方法read_uploader和扩展模块中的write_uploader。我如何必须覆盖这些来制作mount_uploader命令使用我的“虚拟
我正在尝试动态构建一个多维数组。我想要的基本上是这样的(为简单起见写出来):b=0test=[[]]test[b]这给了我错误:NoMethodError:undefinedmethod`test=[[],[],[]]而且它工作正常,但在我的实际使用中,我不会事先知道需要多少个数组。有一个更好的方法吗?谢谢 最佳答案 不需要像您正在使用的索引变量。只需将每个数组附加到您的test数组:irb>test=[]=>[]irb>test[["a","b","c"]]irb>test[["a","b","c"],["d","e","f"]]
如何只加载map边界内的标记gmaps4rails?当然,在平移和/或缩放后加载新的。与此直接相关的是,如何获取map的当前边界和缩放级别? 最佳答案 我是这样做的,我只在用户完成平移或缩放后替换标记,如果您需要不同的行为,请使用不同的事件监听器:在你看来(index.html.erb):{"zoom"=>15,"auto_adjust"=>false,"detect_location"=>true,"center_on_user"=>true}},false,true)%>在View的底部添加:functiongmaps4rail