admin管理员组

文章数量:1335871

I’m building a microservices-based application and in the front-end I'm using React and TypeScript. In one of the services, I fetch post data to display to users. Each post has a user field containing details about the user who created the post. However, if the user service is down, the user field in the response is null.

Here’s an example of the response when the user service is unavailable:

{
  "id": 1,
  "content": "Sample post content",
  "user": null
}

In such cases, I want to display the post without revealing to the user that there’s an issue (like the user service being down). For example, I might display something like "Anonymous" in place of the user’s name. However, I want to avoid explicitly using if-else or ternary operations to handle the null value.

I’ve Tried Using optional chaining, but it still requires me to provide fallback logic like user?.name || "Anonymous" also I’ve Tried Wrapping the logic in a function, but it feels repetitive across components.

What’s the best way to cleanly handle such scenarios in React, ensuring maintainability and avoiding repetitive conditional logic throughout the codebase?

I’m building a microservices-based application and in the front-end I'm using React and TypeScript. In one of the services, I fetch post data to display to users. Each post has a user field containing details about the user who created the post. However, if the user service is down, the user field in the response is null.

Here’s an example of the response when the user service is unavailable:

{
  "id": 1,
  "content": "Sample post content",
  "user": null
}

In such cases, I want to display the post without revealing to the user that there’s an issue (like the user service being down). For example, I might display something like "Anonymous" in place of the user’s name. However, I want to avoid explicitly using if-else or ternary operations to handle the null value.

I’ve Tried Using optional chaining, but it still requires me to provide fallback logic like user?.name || "Anonymous" also I’ve Tried Wrapping the logic in a function, but it feels repetitive across components.

What’s the best way to cleanly handle such scenarios in React, ensuring maintainability and avoiding repetitive conditional logic throughout the codebase?

Share Improve this question asked Nov 19, 2024 at 21:48 Abdallah BenasslouneAbdallah Benassloune 1 1
  • 1 Create a component for your user display logic and use that wherever you display the username. – Oliver Commented Nov 19, 2024 at 21:53
Add a comment  | 

1 Answer 1

Reset to default 1

How about defining a function to fetch your user to always resolve to a User?

Something like:

interface User {
  name: string
}

export const fetchUser = async (): Promise<User> => {
  try {
    const response = await fetch('api/user')
    return (await reponse.json()) as User;
  } catch (error) {
    return { name: 'Anonymous' }
  }
}

Keep in mind that request failures can still be visible in the network tab of the developer console.

本文标签: