我尝试选择一个默认值到一个选择输入,但输入不承认该值,直到我手动更改它。默认情况下,我设置"所有"作为我的默认值。这里是我的代码和codesandbox链接:
import "./styles.css";
import React, { useState, useEffect } from "react";
import { FormField } from "react-form-input-fields";
import "react-form-input-fields/dist/index.css";
export default function App() {
let [type, setType] = useState("All");
const types = [
{ label: "All", value: "All" },
{ label: "Afganistan", value: "Afganistan" },
{ label: "Albania", value: "Albania" },
{ label: "Algeria", value: "Algeria" },
{ label: "American Samoa", value: "American Samoa" },
{ label: "Andorra", value: "Andorra" },
{ label: "Angola", value: "Angola" }
];
function handletype(e) {
setType(e);
}
return (
<div className="App">
{/* <h1>Hello CodeSandbox</h1>
<h2>Start editing to see some magic happen!</h2> */}
<FormField
type="select"
value={type}
option={types}
label={"Select your type"}
keys={"label"}
handleOnChange={(value) => handletype(value)}
/>
</div>
);
}
2条答案
按热度按时间6ju8rftf1#
您使用的库有错误
源代码显示
value
属性仅在componendDidUpdate
中检查,但初始呈现时不会调用此钩子我宁愿使用其他库
gmxoilav2#
一个简单的解决方法是设置
label= [hook value]
Demo