CodexBloom - Programming Q&A Platform

Challenges with displaying dynamic content from a Java backend in a React frontend using Axios

๐Ÿ‘€ Views: 70 ๐Ÿ’ฌ Answers: 1 ๐Ÿ“… Created: 2025-10-05
react axios spring-boot JavaScript

I'm migrating some code and I've been banging my head against this for hours. While refactoring my React application to improve user experience, I'm integrating a Java backend that serves data via REST APIs. The goal is to fetch user-related data dynamically and display it in a user-friendly manner. I'm using Axios for the API calls and managing state with React's useState hook. Here's the function that makes the API call: ```javascript import axios from 'axios'; const fetchUserData = async (userId) => { try { const response = await axios.get(`http://localhost:8080/api/users/${userId}`); setUserData(response.data); } catch (error) { console.error('Error fetching user data:', error); } }; ``` The backend, built with Spring Boot (version 2.5), returns a JSON response that looks like this: ```json { "id": 1, "name": "John Doe", "email": "john.doe@example.com" } ``` When I call this function upon user selection, the data seems to be fetched correctly, but the UI does not update as expected. I suspect it might be related to state management or how I'm triggering the fetch. Currently, I trigger the fetch inside a useEffect like this: ```javascript useEffect(() => { fetchUserData(selectedUserId); }, [selectedUserId]); ``` Despite this setup, I've noticed that the component doesnโ€™t re-render with the new data unless I manually refresh the page. I've also tried adding a loading state to enhance user experience, but that led to additional complexity without solving the underlying issue. Exploring different approaches, I attempted to manage the user state in a parent component and pass it down as props. However, it still didnโ€™t resolve the UI update problem. Any suggestions on how to ensure the frontend dynamically reacts to the data coming from the Java backend? Also, are there any best practices for handling such integrations? I've reviewed relevant documentation and community suggestions but still feel stuck. Insights on effective state management or optimizing the data-fetching process would be greatly appreciated! For context: I'm using Javascript on Linux. Any help would be greatly appreciated! I'm working on a CLI tool that needs to handle this. I'm open to any suggestions.