【发布时间】:2021-10-05 22:02:35
【问题描述】:
您好,我必须遵循简化的代码。我使用 Formik 作为验证。还有 Material Ui 和 Reactjs。 Form、Row 和 Col 标签来自 Material。 FastField 与 InputField 相同。
我想要的是在输入字段中的 onClick 出现一个下拉菜单并显示我使用 axios-Request 获取的数组。
´´´
const url = 'http://localhost:3000';
const [searchValues, setSearchValues] = useState([]);
const getDropdownItems = (event) => {
console.log('event', event.target.getAttribute('id'));
axios
.get(`${url}/${event.target.getAttribute('id')}`)
.then(
(res) => setSearchValues(res),
console.log('restl', searchValues)
);
};
render(
<Form
onFocus={getDropdownItems}
onSubmit={formik.handleSubmit}
>
<Row>
<Col xs="auto" style={minWidth}>
<FastField
id="DatumEingabe"
name="DatumEingabe"
component={Autocomplete}
label="Datum-Eingabe"
type="text"
options={searchValues}
/>
</Col>
</Row>
</Form>
)
当我检查我的控制台时,我从第一个 console.log 中获得了 输入字段。第二个 console.log 说数组是空的, 尽管 res 可用并且应该设置。为什么它不起作用 这边。
【问题讨论】:
-
你这里有一个错字
.then((res) => {setSearchValues(res); console.log('restl', searchValues)});