Two (or more) custom hooks which send rest requests (post, put, get), do something with the results and I want they work sequentially

I have a React application, in which I need:

  1. Set the initial state
  2. Create an object, if the initial object doesn’t exists
  3. Update the created object
  4. Get the updated object

I want to do this with custom hooks and local state. For now I don’t want to use Redux or other state-centrilized solutions

I am a bit confused, because I haven’t been working with React for some time and I forget the concept a bit… I wanted to split the code from one huge piece of code inside one useEffect and use a custom a hook per one rest request plus some logic aroud this.

This is not my real code, but it’s quit close to it.
This is my main component.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>function MyApp() {
const [myState, setMyState] = useState(myInitialObject);
// hook where I create a new object via post request
const { newObject } = useCreateMyNewObject(myState);
// hook where I update the created object via put request, I dont need the result
useUpdateMyNewObject(newObject);
// hook where I call get method
const updatedObject = useFetch(newObject);
return (
<>
{ use properties of updatedObject }
</>
);
}
</code>
<code>function MyApp() { const [myState, setMyState] = useState(myInitialObject); // hook where I create a new object via post request const { newObject } = useCreateMyNewObject(myState); // hook where I update the created object via put request, I dont need the result useUpdateMyNewObject(newObject); // hook where I call get method const updatedObject = useFetch(newObject); return ( <> { use properties of updatedObject } </> ); } </code>
function MyApp() {
  const [myState, setMyState] = useState(myInitialObject);

  // hook where I create a new object via post request
  const { newObject } = useCreateMyNewObject(myState);

  // hook where I update the created object via put request, I dont need the result
  useUpdateMyNewObject(newObject);

  // hook where I call get method
  const updatedObject = useFetch(newObject);

  return (
    <>      
       { use properties of updatedObject }
    </>
  );
}

It calls post method twice, put – ones and get – never.
We use strict mode, and I guess this is why post method is called twice, but I have no idea why get (useFetch hook) doesn’t work.

This is not real code of the useFetch hook:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>const useFetch = (myInitialObject) => {
const [myState, setMyState] = useState(myInitialObject);
useEffect(() => {
if (!myState?.id) {
return;
}
async function fetchFromServer(id) {
const fetchedObject = await fetchObject(id);
setMyState(fetchedObject );
}
fetchFromServer(myState.id)
}, [myState]);
return myState;
};
</code>
<code>const useFetch = (myInitialObject) => { const [myState, setMyState] = useState(myInitialObject); useEffect(() => { if (!myState?.id) { return; } async function fetchFromServer(id) { const fetchedObject = await fetchObject(id); setMyState(fetchedObject ); } fetchFromServer(myState.id) }, [myState]); return myState; }; </code>
const useFetch = (myInitialObject) => {
  const [myState, setMyState] = useState(myInitialObject);

  useEffect(() => {
    if (!myState?.id) {
      return;
    }

    async function fetchFromServer(id) {
       const fetchedObject = await fetchObject(id);
       setMyState(fetchedObject );
    }

    fetchFromServer(myState.id)

  }, [myState]);

  return myState;
};

My question is what do I do wrong?

2

There’s no way for useFetch to wait until the PUT has completed. What’s likely happening:

  • POST creates object
  • newObject probably starts out null and becomes something as a result of POST
  • useUpdateMyNewObject and useFetch kick off at essentially the same time, with the initial null value, and do nothing, then maybe again with the updated value. Seems like useFetch must be short-circuiting, suggesting newObject isn’t getting updated as you expect.

At the very least, I think you need to await the PUT before calling useFetch. Checking (printing to console or watching in the debugger) on the values of newObject between the various calls will likely be helpful.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật