【发布时间】:2025-12-02 04:10:01
【问题描述】:
我的 state 位于我的 APP 级组件中,我正在尝试捕获放入表单组件中的数据。我正在使用钩子,但我无法弄清楚我做错了什么!
我尝试将状态置于表单级别,但它返回未定义,我的最终目标是将此数据发送到 firebase,并将其显示在我的应用程序另一个页面上的表格中。
应用程序
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;
主页组件
import React, { Fragment } from "react";
import InputForm from "./Form";
const Home = props => {
return (
<Fragment>
<InputForm />
</Fragment>
);
};
export default Home;
FORM 组件
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;
【问题讨论】:
-
您没有根据该代码在任何地方导入
Home。那正确吗?我会在App中期待它。 -
如果你想重用表单的处理程序,一个很好的模式可以让你免于丢失 propdrilling 将使用减速器或类似的东西。其他方法是将您的处理程序传递给 InputForm。
-
我刚刚上传了我的 header/router 组件以显示所有内容的导入位置,我正在尝试学习 React Router...
标签: javascript reactjs state react-props reactstrap