У меня есть состояние , расположенное в моем компоненте уровня APP, и я пытаюсь захватить данные, помещаемые в мой компонент формы. Я использую крючки, но я не могу понять, что я делаю неправильно!
Я попытался перевести состояние на уровень формы, но оно возвращает неопределенное значение, моя конечная цель - отправить эти данные в firebase и отобразить их в таблице на другой странице моего приложения.
APP
import React, { useState, createRef } from "react";
import { render } from "react-dom";
import Header from "./components/Header";
import About from "./components/About";
const App = () => {
const [date, setDate] = useState("");
const [name, setName] = useState("");
const [hours, setHours] = useState("");
const [description, setDescription] = useState("");
return (
<div>
<Header />
</div>
);
};
render(<App />, document.getElementById("root"));
** Компонент HEADER / ROUTER
import React from "react";
import { render } from "react-dom";
import {
BrowserRouter as Router,
Switch,
Route,
NavLink,
Link
} from "react-router-dom";
import Home from "./pages/home/Home";
import Data from "./pages/data/Data";
import About from "./pages/about/About";
import Contact from "./pages/contact/Contact";
import "../style.css";
const Header = () => {
return (
<Router>
<div>
<nav className="navbar">
<NavLink className="link nav-item" to="/entry">
Time Entry
</NavLink>
<NavLink className="link nav-item" to="/data">
Data
</NavLink>
<NavLink className="link nav-item-right" to="/about">
About
</NavLink>
<NavLink className="link nav-item-right" to="/contact">
Contact
</NavLink>
</nav>
{/* A <Switch> looks through its children <Route>s and
renders the first one that matches the current URL. */}
<Switch>
<Route exact path="/entry" component={Home} />
<Route exact path="/data" component={Data} />
<Route exact path="/about" component={About} />
<Route exact path="/contact" component={Contact} />
</Switch>
</div>
</Router>
);
};
export default Header;
HOME Компонент страницы
import React, { Fragment } from "react";
import InputForm from "./Form";
const Home = props => {
return (
<Fragment>
<InputForm />
</Fragment>
);
};
export default Home;
ФОРМАЛЬНЫЙ компонент
import React, { Fragment, useState, createRef } from "react";
import { Button, Form, FormGroup, Label, Input, FormText } from "reactstrap";
const InputForm = props => {
const dateEntry = createRef();
const nameEntry = createRef();
const hourEntry = createRef();
const descriptionEntry = createRef();
const handleClick = e => {
e.preventDefault();
setDate(dateEntry.current.value);
setName(nameEntry.current.value);
setHours(hourEntry.current.value);
setDescription(descriptionEntry.current.value);
// console.log(nameEntry.current.value);
};
return (
<Form className="data-entry">
<FormGroup className="form-entry-items">
<Label className="label">Date</Label>
<Input type="date" className="input" ref={dateEntry} />
</FormGroup>
<FormGroup className="form-entry-items">
<Label className="label">Lease Acconting Manager Name</Label>{" "}
<Input type="text" className="input" ref={nameEntry} />
</FormGroup>
<FormGroup className="form-entry-items">
<Label className="label">Hours Worked</Label>{" "}
<Input className="input" ref={hourEntry}/>
</FormGroup>
<FormGroup className="form-entry-items">
<Label className="label">Description</Label>{" "}
<Input type="textarea" className="input" ref={descriptionEntry}/>
</FormGroup>
<Button onClick={handleClick}>Submit</Button>
</Form>
);
};
export default InputForm;