【发布时间】:2021-05-26 07:07:31
【问题描述】:
我正在用 React 构建一个简历创建者。我有一个表单,一个预览组件,它们最接近的父组件是 Main。我想要实现的是,当用户在表单字段中输入时,右侧的预览字段会使用表单中的数据自动更新,但我无法让它工作。这是我在 React 中的第一个项目,我不能使用钩子或函数组件。我知道我做错了什么,但无法确定是什么。
主要组件
import React, { Component } from "react";
import Form from "./form/Form";
import Preview from "./formpreview/Preview";
class Main extends Component {
constructor(props) {
super(props);
this.state = {
firstName: "",
lastName: "",
title: "",
address: "",
phoneNum: "",
email: "",
description: "",
};
this.handleSubmit = this.handleSubmit.bind(this);
this.handleInputChange = this.handleInputChange.bind(this);
}
handleSubmit = (event) => {
event.preventDefault();
};
handleInputChange = (event) => {
this.setState = {
[event.target.name]: event.target.value,
};
};
render() {
const { firstName } = this.state;
return (
<div className="main">
<Form
submitForm={this.handleSubmit}
changeInput={this.handleInputChange}
firstName={firstName} lastName={lastName} title=
{title} address={address} phoneNum={phoneNum}
email={email} description={description}
/>
<Preview onChange={this.handleInputChange}
firstName={firstName} lastName={lastName} title=
{title} address={address} phoneNum={phoneNum}
email={email} description={description}/>
</div>
);
}
}
表单组件
import React, { Component } from "react";
import Personal from './buildingblocks/PersonalInfo';
import Experience from './buildingblocks/Experience';
import Education from './buildingblocks/Education';
import Buttons from './buildingblocks/FormButtons';
class Form extends Component {
constructor(props) {
super(props);
this.state = {
firstName: this.props.firstName,
lastName: this.props.lastName,
title: this.props.title,
address: this.props.address,
phoneNum: this.props.phoneNum,
email: this.props.email,
description: this.props.description
}
}
submitForm = () => {
this.props.submitForm()
}
changeInput = () => {
this.props.changeInput()
}
render() {
const {firstName, lastName, title, address, phoneNum, email, description} = this.state;
return (
<form className="cvForm" onSubmit={this.submitForm} onChange={this.changeInput}>
<Personal firstName={firstName} lastName={lastName} title={title} address={address} phoneNum={phoneNum} email={email} description={description}/>
<Experience />
<Education />
<Buttons />
</form>
);
}
}
export default Form;
预览组件
import React, { Component } from "react";
class Preview extends Component {
constructor(props) {
super(props);
this.state = {
firstName: this.props.firstName,
lastName: this.props.lastName,
title: this.props.title,
address: this.props.address,
phoneNum: this.props.phoneNum,
email: this.props.email,
description: this.props.description
}
}
submitForm = () => {
this.props.submitForm()
}
changeInput = () => {
this.props.changeInput()
}
render() {
const {firstName, lastName, title, address, phoneNum, email, description} = this.state;
return (
<div className="cvPreview">
<div className="gridItem nameItem">
<h1>{firstName} </h1>
<h3>Data engineer</h3>
</div>
...
);
}
}
我没有包含所有预览组件,因为它只是元素。我知道我没有正确执行此操作,我收到 TypeError “无法读取未定义的属性'目标'”,我很确定我不应该多次定义这些道具,但在我尝试了一切之后,这是我的最后一枪。我卡住了,求助。
【问题讨论】:
-
为什么不能使用函数式组件和 React hooks?卡在较旧的 React 版本中?
-
部分课程。我想首先要熟悉类组件。
-
在您的表单组件中,您有
this.props.changeInput()。但是该函数期望事件作为参数。所以你应该有changeInput = (event) => { this.props.changeInput(event) }(虽然我不能100%确定当你将onChange直接附加到<form/>元素时触发的事件类型,所以也许打印出来看看)。 -
当你将类组件从新版本的 react 中移出时,你最终可能会拥有很多不推荐使用的组件...
-
@ChristianMoen AFAIK React 没有提到从 React 中删除类,太多的依赖项。从 2019 年 2 月 6 日开始的这个 blog post 中没有介绍钩子,或者我知道的任何内容。你有引用吗?
标签: javascript reactjs state react-props