【问题标题】:Passing array and display data from API to event calendar将数组和显示数据从 API 传递到事件日历
【发布时间】:2021-02-28 10:49:09
【问题描述】:

我正在使用https://github.com/joshjhargreaves/react-native-event-calendar 制作一个简单的日视图日历以响应本机并从 API 获取数据。我正在使用 axios 获取数据

 const[title,setTitle]=useState('')
    const[start,setStart]=useState('')
    const[end,setEnd]=useState('')
    const[summary,setSummary]=useState('')

  const data={
    title,
    start,
    end,
    summary
  }
  
  const[timetable,setTimetable]=useState([]);

  useEffect(() => {
      getData()
  },[])

  const getData=()=>{
      Axios.get('http://sh3ll.my.id/events/timetable.json')
      .then(res=>{
          console.log('res getData:', res.data)
          setTimetable(res.data)
      })
  }

如何在 events 属性上传递数组,我无法显示来自 api 的数据

      <EventCalendar
          events={??}
          width={width}
          size={60}
          initDate={'2020-11-17'}
          scrollToFirst
        /> 

【问题讨论】:

  • 我认为您必须尝试将您的状态时间表用于道具事件。根据文档,这个道具接受一系列事件。这是你从 timetable.json 中检索到的,对吧?
  • 另外,如果第一次尝试传递状态时间表不起作用,请添加props renderEvent,它定义了组件应如何显示数据。
  • @ebillis 我试过了,没用,是的,我从 timetable.json 获取数据

标签: react-native api axios react-hooks react-native-android


【解决方案1】:

试试这个

const[events,setEvents]=useState([]); // default empty

const getData=()=>{
      Axios.get('http://sh3ll.my.id/events/timetable.json')
      .then(res=>{
          console.log('res getData:', res.data)
          setEvents(res.data.timetable) // set here
      })
  }



<EventCalendar
          events={events} // use here
          width={width}
          size={60}
          initDate={'2020-11-17'}
          scrollToFirst
        /> 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-06-02
    • 2019-05-09
    • 2021-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-03
    • 1970-01-01
    相关资源
    最近更新 更多