【发布时间】:2019-05-05 19:37:17
【问题描述】:
我想动态生成TextFields,然后将它们的值存储在状态的数组中。
我的进口:
import TextField from '@material-ui/core/TextField';
import Button from '@material-ui/core/Button';
我在 TextField 下方创建了一个按钮。我想在单击按钮后动态添加 TextFields。
TextField required={true}
onChange={e => this.state.book_authors.push(e.target.value)}
className={this.props.classes.textField}
label={"Author"}
variant="outlined"
type="text"/>
<Button
variant="contained" color="primary"
action={() => this.handleAddAuthorField()}
className={this.props.classes.button}
>
Add Author
</Button>
我的方法:
handleAddAuthorField(): JSX.Element {
return (
<Row>
<TextField
required={true}
onChange={e => this.state.book_authors.push(e.target.value)}
className={this.props.classes.textField}
label={"Author"}
variant="outlined"
type="text"
/>
</Row>
)
}
状态 book_authors 将 TextField 输入存储为一个数组。
public state: IsBookState = {
book_authors: [],
};
单击按钮后,TextField 没有生成。
【问题讨论】:
标签: reactjs typescript material-ui