数据加载完成后如何使用钩子设置初始状态

How to set an initial state with hooks when data finish to load

我正在尝试将 full_name 设置为我的初始状态,但我变得不确定。当我将数据发送到我的 props.userDetailsFetchRequest(id, data); 时,我无法检查这一点,如何在数据加载完成时设置此初始状态。

 const UserDetails = props => {
            const classes = useStyles();

            const { userDetailsLoading } = props;
            const { full_name } = props.userDetails;

            console.log('loading:', userDetailsLoading)
            console.log('full name:', full_name)

            const [name, setName] = useState(full name); <-------- I want this inicialize with
                                                                         michele sena riveira  

          function handleSubmit(e) {
            e.preventDefault();

            const { id } = props.userDetails;

            const data = {
                "full_name": name,
            }

            props.userDetailsFetchRequest(id, data); <--- I'm sending this request to my saga
    and I'm getting full_name as undefined.
        }


传奇

function* userDetailsEditFetchRequest({ id, data }) {
       console.log("full_name in saga:",data)
}

您将为此使用 useEffect

useEffect(() => {
    setName(full_name)
},[full_name]

现在,每当 full_name 发生变化时,无论道具的值是多少,效果都会更新您的状态值。

然而,最好尽可能直接使用道具,以消除任何可能的差异