简体   繁体   English

在 useEffect 上重新渲染组件

[英]Make a component Rerender on useEffect

I'm creating a search bar, which calls an API to return a list of devices with matching names.我正在创建一个搜索栏,它调用 API 以返回具有匹配名称的设备列表。 Ideally, when the user first looks at the component, it sees just a search bar.理想情况下,当用户第一次查看组件时,它只会看到一个搜索栏。 Once the user types in the search bar, the API returns a list of matching names.一旦用户在搜索栏中键入内容,API 就会返回匹配名称的列表。 A list of these names is then shown below the search bar.然后在搜索栏下方显示这些名称的列表。

I'm trying to do this with hooks, but I can't get the list to show / the component to update and show the new list.我正在尝试使用钩子来执行此操作,但无法获取要显示的列表/要更新的组件并显示新列表。

What am I missing, is this the correct way to go about this?我错过了什么,这是解决这个问题的正确方法吗?

const Search = () => {
  const [input, setInput] = useState("");
  let devices = [];
  const handleChange = e => {
    setInput(e.target.value.toUpperCase());
  };
  useEffect(() => {
    apiService.getDevices(input).then(response => {
      console.log("response:", response); // This brings back the response correctly
      const newDevices = response.map(device => <li key={device}>{device}</li>);
      devices = <ul>{newDevices}</ul>;
    });
  }, [input]);

  return (
    <Fragment>
      <div>
        <div className="form-group">
          <div className="form-group__text">
            <input
              type="search"
              onChange={handleChange}
              placeholder="Search device by serial number"
            />
            <button type="button" className="link" tabIndex="-1">
              <span className="icon-search"></span>
            </button>
          </div>
        </div>
        <div>{devices}</div>
        <p>testestes</p>
      </div>
    </Fragment>
  );
};

Store devices in state, and then do the map rendering directly in the return, like so:将设备存放在 state 中,然后直接在 return 中进行地图渲染,如下所示:

const Search = () => {
  const [input, setInput] = useState("");
  const [devices, setDevices] = useState([]);
  const handleChange = e => {
    setInput(e.target.value.toUpperCase());
  };
  useEffect(() => {
    apiService.getDevices(input).then(response => {
      setDevices(response);
    });
  }, [input]);

  return (
    <Fragment>
      <div>
        <div className="form-group">
          <div className="form-group__text">
            <input
              type="search"
              onChange={handleChange}
              placeholder="Search device by serial number"
            />
            <button type="button" className="link" tabIndex="-1">
              <span className="icon-search"></span>
            </button>
          </div>
        </div>
        <div>
          <ul>
            {devices.map(device => <li key={device}>{device}</li>)}
          </ul>
        </div>
        <p>testestes</p>
      </div>
    </Fragment>
  );
};

A component will only re-render when the props or state change, so a useEffect cannot trigger a rerender without also updating some state组件只会在 props 或 state 改变时重新渲染,所以useEffect不能在不更新某些状态的情况下触发重新渲染

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM