【问题标题】:react-native-calendars jump to a selected monthreact-native-calendars 跳转到选定的月份
【发布时间】:2021-12-31 17:43:23
【问题描述】:

我正在使用 react-native-calendars。 我有一个<Calendar/> 组件。

我已经阅读了数十次文档,但找不到比 current 属性更好的更改月份的属性,它初始化了日历的视图。例如,如果我设置current={"2014-01-01"},我会看到 2014 年 1 月 1 日的日历。问题是我无法重新渲染这个道具。

我也尝试使用key 道具,但它不会更改日期。 Ref 也不是一个选项,我有自定义 <CalendarHeader/>

package.json

 "react": "17.0.2",
 "react-native": "0.66.4",
 "react-native-calendars": "1.1274.0"

Calendar.tsx

const CalendarScreen = () => {
  const [current, setCurrent] = useState('2020-01-01');

return (
        <Calendar
          current={current}
          customHeader={CalendarHeaderHandler}
          style={styles.calendar}
        />
        <TouchableOpacity onPress={() => setCurrent('2021-01-01')}>
            <Text>2021</Text>
        </TouchableOpacity>
 )};

【问题讨论】:

    标签: react-native react-native-calendars


    【解决方案1】:

    通过添加两个道具keycurrent 解决了这个问题

    Prop key 触发重新渲染

      const CalendarScreen = () => {
          const [current, setCurrent] = useState('2020-01-01');
    
      return(
            <Calendar
              current={current}
              key={current}
              customHeader={CalendarHeaderHandler}
              style={styles.calendar}
            />
            <TouchableOpacity onPress={() => setCurrent('2021-01-01')}>
                <Text>2021</Text>
            </TouchableOpacity>
       )};
    

    【讨论】:

      猜你喜欢
      • 2023-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多