简体   繁体   English

使用自定义挂钩时,如何在 useEffect 中的操作后设置默认值

[英]How can I set default value after actions in useEffect when I use custom hooks

I use React with Hooks and Redux saga.我将 React 与 Hooks 和 Redux saga 结合使用。 Once page is rendered, useEffect works and take actions(getting company information) and then, this page re-rendered.页面呈现后,useEffect 工作并采取行动(获取公司信息),然后重新呈现此页面。 Then, variable selCompany from rootState has data.然后,来自 rootState 的变量 selCompany 有数据。 But, variables like name and location which ared used with useInput don't set initialData from selCompany's data.但是,与 useInput 一起使用的 name 和 location 等变量不会从 selCompany 的数据设置 initialData。 How can I set default value in useInput(custom hooks) after actions in useEffect?在 useEffect 中执行操作后,如何在 useInput(custom hooks) 中设置默认值?

Here is custom hooks which I use.这是我使用的自定义钩子。

/*useInput.ts*/
import { useState, useCallback } from "react";
// custom hook
export default function useInput(initialValue: string) {
  const [value, setValue] = useState(initialValue);
// after re-rendered, variable [value] is empty string
// but parameter [initialValue] has data
// console.log(initialValue); // 'test'
// console.log(value); // ''

  const handleChange = useCallback((event: React.ChangeEvent<HTMLInputElement>) => {
    setValue(event.target.value);
  }, []);
  return [value, handleChange] as [string, typeof handleChange];
}

And Here is codes这是代码

/**CompanyUpdateContainer.tsx */
const CompanyUpdateContainer: React.SFC<RouteComponentProps<MatchParams>> = ({ match }) => {
  const { email } = useSelector((state: RootState) => state.user);
  const { selCompany } = useSelector((state: RootState) => state.company);
  const { id } = match.params;
  const [name, setName] = useInput(selCompany.name);
  const [location, setLocation] = useInput(selCompany.location);
  const [error, setError] = useState<Array<Error>>([]);
  const [imageList, setImageList] = useState<Array<File>>(selCompany.imageList);
  const [loading, setLoading] = useState(false);
  const dispatch = useDispatch();

  console.log(name, location); //after re-render, still values return empty string
  useEffect(() => {
    setLoading(true);
    dispatch(getCompanyDetail({ email, id })); // getComapnyDetail put data on 'selCompany'
    setLoading(false);
    return () => {};
  }, [dispatch, email, id]);
..... some mode code

you could set the value to initialValue when the component mounts您可以在组件安装时将该value设置为initialValue

 /*useInput.ts*/
    import {
        useState,
        useCallback,
        useEffect
    } from "react";
    // custom hook
    export default function useInput(initialValue:string) {
        const [value, setValue] = useState(initialValue);

 useEffect(() => {
   setValue(initialValue);
  },[]);

  const handleChange = useCallback((event: React.ChangeEvent <HTMLInputElement > ) => {
       setValue(event.target.value);
        }, []);
        return [value, handleChange] as[string, typeof handleChange];
    }

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

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