javascript - 状态已更改但视图未更新 React Native
问题描述
我正在使用。'react-native-calendars'
我的目标是显示点,然后在用户按下时标记多个日期。标记点是成功的。我使用markedDates
对象作为状态变量,但是当更新markedDates
对象以添加'selected':true, 'marked':true
属性时calendar
没有呈现它,我检查了markedDates
对象,一切似乎都很好。
下面是代码
<Calendar style={{width:'100%',height:300}}
markingType={'multi-dot'}
onDayPress={(day) => {
this.handleConfirm2(day);
}}
markedDates={this.state.markedDates}
/>
下面的handleConfirm2方法
var calanderObj = this.state.markedDates;
let o = calanderObj ['2021-09-02'];// adding properties to this spec date for testing
o['selected'] = true;
o['mark'] = true;
calanderObj['2021-09-02'] = o;
this.setState({markedDates:calanderObj},function (){
console.log("after, state changed but calendar not displaing ",this.state.markedDates);
});
解决方案
试试这种方式(更新)
/* This will create the clone of the object with a new ref,
so whenever the ref is updated, the view reflects */
const calanderObj = {...this.state.markedDates};
let o = calanderObj ['2021-09-02'];// adding properties to this spec date for testing
o['selected'] = true;
o['mark'] = true;
calanderObj['2021-09-02'] = o;
this.setState({markedDates:calanderObj},function (){
console.log("after, state changed but calendar not displaing ",this.state.markedDates);
});
推荐阅读
- postgresql - Azure Postgresql 数据库诊断设置中没有可见的事件中心命名空间
- java - 了解自定义 JRE(Java 中的模块)
- laravel - 授权标头未从 vue js 门户传递,尽管使用 axios.defaults.withCredentials = true
- django - Django:使用reactJS发送请求时出现错误请求
- python-3.x - Pygrib UnicodeEncodeError
- linux - 如何在 Bash 中增加连接端口?
- vue.js - 如何在 vuetify.js 中添加形状分隔符
- sharepoint - 如何对 feld(国家和城镇)进行两次查找 - Sharepoint List - 添加新对象
- bitcoin - 矿池挖矿为什么不能同时在多个矿池中挖矿?
- ios - WKWebView 只允许 DOM 主体内的点击事件 [Objective-C]