微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

反应本机渲染按钮

如何解决反应本机渲染按钮

我想我知道这个问题,但是我没有解决办法, 所以我有这些按钮是在useState arrray上循环创建的

const [booleanMonth,setMonth]=useState([
  {key:0,value:'January',title:'Jan',checked:false},{key:1,value:'February',title:'Feb',{key:2,value:'march',title:'Mar',{key:3,value:'April',title:'Apr',{key:4,value:'May',title:'May',{key:5,value:'June',title:'Jun',{key:6,value:'July',title:'Jul',{key:7,value:'August',title:'Aug',{key:8,value:'September',title:'Sep',{key:9,value:'October',title:'Oct',{key:10,value:'November',title:'Nov',{key:11,value:'December',title:'Dec',checked:false}
])

然后渲染按钮:

const createButtonMonth = useMemo(() =>{
console.log('createButtonMonth')
return (<View style={styles.containerForButtons2}>
        {
          booleanMonth.map((item,key) => 
            <View key={item.key} style={styles.buttonFilter3}>
            <Button style={styles.buttonFilter3}
            title={item.title} 
            value={booleanMonth[item.key].checked} 
            onCheckColor='red'
            color={booleanMonth[item.key].checked==true ? 'green':'black'}
            onPress={()=>onPressMonthFilter(booleanMonth[item.key].key,booleanMonth[item.key].checked)}
            /></View>)
        }
        </View>)
  },[booleanMonth])

在这是onPress函数,它将检查状态更改为true或false,因此应该 更改这些按钮的颜色

const onPressMonthFilter = (keyMonth,statusMonth) =>{
if(booleanMonth[keyMonth].checked==true){
  booleanMonth[keyMonth].checked=false
  console.log(booleanMonth)
  setMonth(booleanMonth)
}else{
  booleanMonth[keyMonth].checked=true
  console.log(booleanMonth)
  setMonth(booleanMonth)
}
setMonth(booleanMonth)
}

在控制台日志中,它返回带有选中的true或false,这表示我的代码是正确的,但是没有呈现正确的颜色,false = black,true = green

在这里帮助新手reactjs用户

解决方法

我认为是由于记忆。

尝试删除useMemo,因为它会捕获组件并阻止其重新呈现。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?