英文:
How to set a default value for a Select component from react with MenuItem as options
问题
以下是您提供的内容的翻译部分:
我一直在思考如何在使用MenuItem组件作为选项时,在React中设置Select的默认值。
首先,我使用以下的useEffect获取用户列表,该useEffect返回用户列表(包括id、firstName和lastName等属性):
const [userList, setUserList] = useState([]);
// 获取用户
useEffect(() => {
userService
.listUsersFromTeamRHandAdmin()
.then((res) => {
setUserList(res)
});
}, [])
const {
setFieldValue
} = useFormikContext();
const handleChange = (e) => {
setFieldValue("user.id", setUserList[e.target.value].id)
}
我有以下的Select组件,我想将UserList的第一个用户的id作为默认值,并显示该用户的firstname和lastname。
<Select
name="user"
defaultValue={userList[0].id || ''}
onChange={(e) => handleChange(e)}
style={{
width: "100%",
marginBottom: "1em"
}}
>
{
Object.keys(userList).map((e, keyIndex) => {
return (
<MenuItem key={keyIndex} value={e}>
{userList[e].firstName} {userList[e].lastName}
</MenuItem>
);
})
}
</Select>
有谁知道我如何做到这一点?
编辑:
例如,UserList数组可能包含如下内容:
0: Object { id: 1, firstName: "Britta", lastName: "Perry", … }
1: Object { id: 2, firstName: "Troy", lastName: "Barnes", … }
英文:
I've been wondering how to set the default value of a Select in React when we use the MenuItem component as options.
First, I get an user list with the following useEffect that returns a list of users (objects with id, firstName and lastName among other attributes)
const [userList, setUserList] = useState([]);
//Get users
useEffect(() => {
userService
.listUsersFromTeamRHandAdmin()
.then((res) => {
setUserList(res)
});
}, [])
const {
setFieldValue
} = useFormikContext();
const handleChange = (e) => {
setFieldValue("user.id", setUserList[e.target.value].id)
}
I have the following Select component, where I would like to have the first user's id of the UserList as a default value, with this user's firstname and lastname displayed.
<Select
name="user"
defaultValue={userList[0].id || ''}
onChange={(e) => handleChange(e)}
style={{
width: "100%",
marginBottom: "1em"
}}
>
{
Object.keys(userList).map((e, keyIndex) => {
return (
<MenuItem key={keyIndex} value={e}>
{userList[e].firstName} {userList[e].lastName}
</MenuItem>
);
})
}
</Select>
Does anyone know how I can do that ?
Edit:
For example, the array of UserList would contain something like:
0: Object { id: 1, firstName: "Britta", lastName: "Perry", … }
1: Object { id: 2, firstName: "Troy", lastName: "Barnes", … }
答案1
得分: 1
To have a defaultValue in the MaterialUi Select Component, you should provide a defaultValue that exists in your choices. Please try this:
import "./styles.css";
import { Select, MenuItem } from "@mui/material";
export default function App() {
const arr = [
{ id: 1, firstName: "Britta", lastName: "Perry" },
{ id: 2, firstName: "Troy", lastName: "Barnes" }
];
return (
<div className="App">
<Select
name="user"
defaultValue={arr[0].firstName + arr[0].lastName || ""}
onChange={(e) => handleChange(e)}
style={{
width: "100%",
marginBottom: "1em"
}}
>
{arr.map((el) => (
<MenuItem key={el.id} value={el.firstName + el.lastName}>
{el.firstName + el.lastName}
</MenuItem>
))}
</Select>
</div>
);
}
You can add a separator between firstName and lastName but keep the same syntax in defaultValue too.
英文:
To have a defaultValue in the MaterialUi Select Component you should provide a defaultValue that exist in your choices, please try this :
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
import "./styles.css";
import { Select, MenuItem } from "@mui/material";
export default function App() {
const arr = [
{ id: 1, firstName: "Britta", lastName: "Perry" },
{ id: 2, firstName: "Troy", lastName: "Barnes" }
];
return (
<div className="App">
<Select
name="user"
defaultValue={arr[0].firstName + arr[0].lastName || ""}
onChange={(e) => handleChange(e)}
style={{
width: "100%",
marginBottom: "1em"
}}
>
{arr.map((el) => (
<MenuItem key={el.id} value={el.firstName + el.lastName}>
{el.firstName + el.lastName}
</MenuItem>
))}
</Select>
</div>
);
}
<!-- end snippet -->
As you can see, the defaultValue and the menuItem have the same syntaxe, this a working demo that I've created for this
you can add separator between firstName and lastName but keep the same syntaxe in defaultValue too.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论