javascript - nextjs passing data to component - Stack Overflow

I can't figure out for the life of me to pass the data I fetched from my API to the canvas ponent.

I can't figure out for the life of me to pass the data I fetched from my API to the canvas ponent. It seems to me where the data is available in the handleSubmit() function it is out of scope to the canvas prop. I appreciate any help. Thanks.

I have a datepicker that passes the selected value as a parameter to the API. The data is fetched just fine. I can console.log it but I can't seem to find a way to send the data to my canvas ponent.

import React, { useState, useEffect } from 'react'
import Layout from '../ponents/layout'
import Sidebar from '../ponents/sidebar'
import DatePicker from 'react-datepicker'
import Canvas from '../ponents/canvas'

import 'react-datepicker/dist/react-datepicker.css'

export default function Ground({ track }) {
  function epoch(date) {
    date = Date.parse(date)
    date = date / 1000
    return date
  }

  const [startDate, setStartDate] = useState(new Date())
  const rightThisSecond = Math.round(Date.now() / 1000)
  const beginDate = Math.round(Date.now(startDate) / 1000)

  const handleSubmit = async (e) => {
    e.preventDefault()
    // console.log(epoch(startDate))

    const beginDate = epoch(startDate)
    // console.log(beginDate)
    // ... submit to API or something
    const res = await fetch(
      `/api/tracks/timeseries/${beginDate}/${rightThisSecond}`
    )
    const track = await res.json()
    // console.log(track)
  }
  return (
    <section>
      <h2>Layout Example (Ground)</h2>
      <form onSubmit={handleSubmit}>
        <DatePicker
          selected={startDate}
          onChange={(date) => setStartDate(date)}
        />
        <button type='submit'>Submit</button>
      </form>

      <section>
        <>
          <Canvas data={track} /> // doesn't seem to reach here.
        </>
      </section>
    </section>
  )
}

Ground.getLayout = function getLayout(page) {
  return (
    <Layout>
      <Sidebar />
      {page}
    </Layout>
  )
}

I can't figure out for the life of me to pass the data I fetched from my API to the canvas ponent. It seems to me where the data is available in the handleSubmit() function it is out of scope to the canvas prop. I appreciate any help. Thanks.

I have a datepicker that passes the selected value as a parameter to the API. The data is fetched just fine. I can console.log it but I can't seem to find a way to send the data to my canvas ponent.

import React, { useState, useEffect } from 'react'
import Layout from '../ponents/layout'
import Sidebar from '../ponents/sidebar'
import DatePicker from 'react-datepicker'
import Canvas from '../ponents/canvas'

import 'react-datepicker/dist/react-datepicker.css'

export default function Ground({ track }) {
  function epoch(date) {
    date = Date.parse(date)
    date = date / 1000
    return date
  }

  const [startDate, setStartDate] = useState(new Date())
  const rightThisSecond = Math.round(Date.now() / 1000)
  const beginDate = Math.round(Date.now(startDate) / 1000)

  const handleSubmit = async (e) => {
    e.preventDefault()
    // console.log(epoch(startDate))

    const beginDate = epoch(startDate)
    // console.log(beginDate)
    // ... submit to API or something
    const res = await fetch(
      `/api/tracks/timeseries/${beginDate}/${rightThisSecond}`
    )
    const track = await res.json()
    // console.log(track)
  }
  return (
    <section>
      <h2>Layout Example (Ground)</h2>
      <form onSubmit={handleSubmit}>
        <DatePicker
          selected={startDate}
          onChange={(date) => setStartDate(date)}
        />
        <button type='submit'>Submit</button>
      </form>

      <section>
        <>
          <Canvas data={track} /> // doesn't seem to reach here.
        </>
      </section>
    </section>
  )
}

Ground.getLayout = function getLayout(page) {
  return (
    <Layout>
      <Sidebar />
      {page}
    </Layout>
  )
}

Share Improve this question asked Aug 14, 2021 at 21:38 rf guyrf guy 4392 gold badges11 silver badges29 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 2

Your handleSubmit is async so const track = await res.json() will execute just fine, after the ponent has already rendered and it'll never be used.

You need to update the state after your data returns so that the ponent can rerender with the fetched data, using something like setTrack(track). Then in your ponent you can pass the data from the state.

Example:

const [track, setTrack] = useState()

...

setTrack(await res.json())

...

<Canvas data={track} />

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744703569a4588916.html

相关推荐

  • javascript - nextjs passing data to component - Stack Overflow

    I can't figure out for the life of me to pass the data I fetched from my API to the canvas ponent.

    1天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信