我想在下面显示选定的值以返回所选值,但我无法从中获得数据,这可能是一个小问题

发布于 2025-01-23 07:47:05 字数 2188 浏览 4 评论 0原文


    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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沒落の蓅哖 2025-01-30 07:47:05

更改您的Onchange FN。

onChange={(event, v) => {     
   setSaveResult(v)  
 }
}

OnChange您正在调用一个匿名函数,并且该函数正在执行SetSaveresult,并且您将两个参数传递给匿名FN,事件和值。

Change your onChange fn.

onChange={(event, v) => {     
   setSaveResult(v)  
 }
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文