[英]How to pass TextField with entered data into form after clicking a button?
I have a TextField and a Button (both are material-ui components) displaying on my main page. 我的主页上显示了一个TextField和一个Button(均为material-ui组件)。 I want to be able to click the button and populate a form that includes the previous TextField and any text that had already been written in it. 我希望能够单击该按钮并填充一个包含以前的TextField和已在其中写入的任何文本的表单。 The code I currently have just makes a new instance of the TextField within the form, while keeping the original TextField as well. 我目前拥有的代码只是在表单中创建了TextField的新实例,同时还保留了原来的TextField。 How can I bring the existing TextField over into the form without duplicating? 如何将现有的TextField带入表单而不进行复制?
FormTextField.js FormTextField.js
const FormTextField = props => {
return (
<TextField
fullWidth={true}
multiline={true}
rows="10"
variant="outlined"
>
{props.data}
</TextField>
)
}
export default class FormTextField extends Component {
render() {
data={this.props.data} />
return (
{FormTextField}
);
}
};
Form.js Form.js
const Form= () => {
return (
<FormLabel">Input Text...</FormLabel>
<FormTextField />
);}
export default Form;
App.js App.js
const AddButton= (props) => {
return (
<Button variant="contained" onClick={props.onClick}>
New Interaction
</Button>
)
}
export default class App extends Component {
constructor(props) {
super(props);
this.state = {show: false};
}
showForm = () => {
this.setState({
show: true,
});
}
render() {
return(
<Fragment>
<Header />
<FormTextField />
<AddButton onClick={this.showInteractionForm} />{this.state.show ?
<Form /> : null}
</Fragment>
);
}
};
As you want to share data between two components you can resolve this in different ways, based in your code, a solution could be: 当您要在两个组件之间共享数据时,可以根据您的代码以不同的方式解决此问题,解决方案可以是:
Your App
control the data so, 您的App
控制数据,
in your state can add: 在您的状态下可以添加:
this.state = {
inputData = '';
}
You need to pass an update function to your FromTextField
您需要将更新函数传递给FromTextField
<FormTextField onTextUpdate={(text) => this.setState({ inputData: text })} />
Your form field must be controlled by App
so you need to pass the data to be shown too: 您的表单字段必须由App
控制,因此您也需要传递数据才能显示:
<FormTextField data={this.state.inputData} onTextUpdate={(text) => this.setState({ inputData: text })} />
(you need to add that modification to FormTextField, they are easy) (您需要将该修改添加到FormTextField中,这很容易)
And the last step is to do the same with Form
最后一步是对Form
进行相同操作
<Form data={this.state.inputData} onTextUpdate={(text) => this.setState({ inputData: text })} />
Inside Form
you need to pass data
and onTextUpdate
to the FormTextField
在Form
内部,您需要将data
和onTextUpdate
传递给FormTextField
You can refactor (text) => this.setState({ inputData: text })
to be a method in class. 您可以将(text) => this.setState({ inputData: text })
重构为类中的方法。
EDIT 编辑
https://codesandbox.io/embed/stackoverflow-form-react-9188m you can find the implementation about I told you before. https://codesandbox.io/embed/stackoverflow-form-react-9188m您可以找到我之前告诉您的实现。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.