【发布时间】:2018-08-01 23:47:47
【问题描述】:
我有这个反应代码,它抓取用户输入的文本的一部分,并创建 JSON 对象作为选择标签的选项。这里 textContent 来自具有用户输入的 fileReader,而 handleChange 与表相关。我收到此错误:
TypeError: _this6.state.textContent.match 不是函数
当我 console.log JSON.stringify(this.state.textContent.match(/[A-Z]+:[0-9]{1,3}/gm)) 我得到这个对象 ["A:100","B:300","C:400","D:900","E:800"] 这是正确的,但我不能将它作为选项传递。谁能帮忙?
class AllForm extends Component {
constructor(props) {
super(props);
this.state = {
textContent: ""
};
}
handleChanges = idx => e => {
const { name, value } = e.target;
const rows = [...this.state.rows];
rows[idx] = {
[name]: value
};
};
render() {
return (
<Select
name={"BIN"}
placeholder={"choose the value"}
options={JSON.stringify(
this.state.textContent.match(/[A-Z]+:[0-9]{1,3}/gm)
)}
controlFunc={this.handleChanges(idx)}
/>
);
}
}
选择组件是:
import React from "react";
import PropTypes from "prop-types";
const Select = props => (
<div className="form-group">
<select
name={props.name}
value={props.selectedOption}
onChange={props.controlFunc}
className="form-select"
>
<option value="">{props.placeholder}</option>
{props.options.map(opt => {
return (
<option key={opt} value={opt}>
{opt}
</option>
);
})}
</select>
</div>
);
Select.propTypes = {
title: PropTypes.string.isRequired,
name: PropTypes.string.isRequired,
options: PropTypes.array.isRequired,
selectedOption: PropTypes.string,
controlFunc: PropTypes.func.isRequired,
placeholder: PropTypes.string
};
export default Select;
【问题讨论】:
标签: javascript reactjs