Answer the question
In order to leave comments, you need to log in
How to pass actual data in props, and not from the previous render?
Actually, the code (of course, I cut out everything superfluous):
import React, { useState, useEffect } from 'react';
import ReactDOM from 'react-dom';
import DatePicker from 'react-datepicker';
import "react-datepicker/dist/react-datepicker.css";
export default function DataOutput() {
const [startDate, setStartDate] = useState(new Date());
const [endDate, setEndDate] = useState(new Date());
return (
<div id="dataContainer">
{/* Первый календарь для отметки начала временного промежутка */}
<DatePicker
selected={startDate}
onChange={date => setStartDate(date)}
/>
{/* Второй календарь для отметки конца временного промежутка */}
<DatePicker
selected={endDate}
onChange={date => setEndDate(date)}
minDate={startDate}
/>
<Table startDate={startDate} endDate={endDate} />
</div>
);
}
function Table(props) {
const [data, setData] = useState([]);
const [startDate, setStartDate] = useState(props.startDate);
const [endDate, setEndDate] = useState(props.endDate);
useEffect(() => {
setStartDate(props.startDate);
setEndDate(props.endDate);
{/* Тут я принимаю данные с сервера и записываю в setData(data) */ }
{/* Надо брать с сервера данные за определённый временной промежуток и выдавать их в таблице */ }
fetch('api.ru/?startDate' + startDate + 'endDate' + endDate).then(setData(data));
}, [props.startDate, props.endDate]);
return (
<table>
{/* В отдельной ф-ции генерю <tr>'ки, но тут это ни к чему, лишний код я просто повырезал */}
<TableRows items={data} />
</table>
);
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question