我想在下面显示选定的值以返回所选值,但我无法从中获得数据,这可能是一个小问题
import { Autocomplete, Stack, TextField } from "@mui/material";
import { Box } from "@mui/system";
import axios from "axios";
import React, { useEffect, useState } from "react";
const SearchProperty = () => {
const [searchResult, setSearchResult] = useState([]);
const { saveResult, setSaveResult } = useState('');
console.log(saveResult)
useEffect(() => {
axios
.get(`api/societies/`, {
headers: {
Authorization: `Bearer ${localStorage.getItem("token")}`,
},
})
.then((res) => {
setSearchResult(res.data);
});
}, []);
return (
<div className="p-5 px-md-20 px-2xl-40 ">
<Stack>
<Autocomplete
id="SearchProperty"
getOptionLabel={(searchResult) => `${searchResult.name}`}
options={searchResult}
inputValue={saveResult}
isOptionEqualToValue={(option, value) => option.name === value.name}
onChange={(event, value) => setSaveResult(value)}
noOptionsText={"No Such Property Available"}
renderOption={(props, searchResult) => (
<Box component="li" {...props} key={searchResult.society_id}>
{searchResult.name}
</Box>
)}
renderInput={(params) => (
<TextField fullWidth {...params} label="Search Property By Name" onChange={({ target }) => setSaveResult(target.value)}/>
)}
/>
</Stack>
<div>
<div className="flex">
{
<div>
<h2>{saveResult?.name}</h2>
<h2>{saveResult?.society_id}</h2>
<h2>{saveResult?.name}</h2>
</div>
}
</div>
</div>
</div>
);
};
export default SearchProperty;
我的API工作正常 我可以看到下拉搜索结果 我可以选择结果 但是我无法在我的状态下保存 onChange = {(event,value)=&gt; console.log(value)} 这
onChange={(event, value) => setSaveResult(value)}
无效 我有这个错误 Untuff typeError:SetSaveresult不是功能,
这是我使用MUI自动完成的React代码
import { Autocomplete, Stack, TextField } from "@mui/material";
import { Box } from "@mui/system";
import axios from "axios";
import React, { useEffect, useState } from "react";
const SearchProperty = () => {
const [searchResult, setSearchResult] = useState([]);
const { saveResult, setSaveResult } = useState('');
console.log(saveResult)
useEffect(() => {
axios
.get(`api/societies/`, {
headers: {
Authorization: `Bearer ${localStorage.getItem("token")}`,
},
})
.then((res) => {
setSearchResult(res.data);
});
}, []);
return (
<div className="p-5 px-md-20 px-2xl-40 ">
<Stack>
<Autocomplete
id="SearchProperty"
getOptionLabel={(searchResult) => `${searchResult.name}`}
options={searchResult}
inputValue={saveResult}
isOptionEqualToValue={(option, value) => option.name === value.name}
onChange={(event, value) => setSaveResult(value)}
noOptionsText={"No Such Property Available"}
renderOption={(props, searchResult) => (
<Box component="li" {...props} key={searchResult.society_id}>
{searchResult.name}
</Box>
)}
renderInput={(params) => (
<TextField fullWidth {...params} label="Search Property By Name" onChange={({ target }) => setSaveResult(target.value)}/>
)}
/>
</Stack>
<div>
<div className="flex">
{
<div>
<h2>{saveResult?.name}</h2>
<h2>{saveResult?.society_id}</h2>
<h2>{saveResult?.name}</h2>
</div>
}
</div>
</div>
</div>
);
};
export default SearchProperty;
my api is working fine
i am able to see search result in dropdown
i can select the result
but i can't save it in my state
onChange={(event, value) => console.log(value)}
this works
onChange={(event, value) => setSaveResult(value)}
this doesn't work
i am getting this error
Uncaught TypeError: setSaveResult is not a function
this is A REACT Code I am using MUI Autocomplete
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
更改您的Onchange FN。
OnChange您正在调用一个匿名函数,并且该函数正在执行SetSaveresult,并且您将两个参数传递给匿名FN,事件和值。
Change your onChange fn.
OnChange you are calling a anonymous function and that function is doing setSaveResult, and you are passing two arguments to anonymous fn, event and value.