PRA#01_React-EmmaAlonsoMcCoy-ComponentsFrontendDevelopment - #7
Open
emsmccoy wants to merge 31 commits into
Open
Conversation
…s with conditional rendering
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PRA#01-React Components Frontend Development
Overview
This document serves as both a guide and a log for the PRA#01 React frontend development task.
Images and a video demo can also be found in the PRA folder
PR Submission Checklist
Tasks Completed:
Common
Postman mock server is set up with correct endpoint.
Axios function is implemented in a decoupled manner.
User JSON structure includes varied data types.
User component renders correctly with avatar.
Difficulty levels and filtering are functional.
Synonyms rendering is implemented and error-free.
Optional
CSS Enhancements: transient effect on hover
Animation Libraries
Enhanced Card Features
Showcase: Video and Images
Link to Video Demo


Estimated Time for Tasks
Common Part
Error rendering due to useEffect() and find()
Optional Part
Error Documentation and Solution
Error:
TypeError: Cannot read properties of undefined (reading 'map')Corresponding task:
Description: This error occurs when trying to use the
.map()function on a variable that is eitherundefinedornull. In JavaScript,map()is a method available only on arrays, and attempting to call it on something that is not an array (or is undefined) will result in this error.Error Trace:
WordListCards.jsxWordList)WordList (Cards.jsx:40:18)AppPossible Causes:
.map()isundefinedornull..map().Solution:
The error was resolved by modifying the
data-api.jsfile. The issue was in how the data was being returned from the API call.Original code indata-api.js:Updated code in
api.js:Explanation:
The API was returning the array of words directly in the
response.data, not nested under awordsproperty. By changingreturn response.data.words;toreturn response.data;, we correctly access the array of words, allowing the.map()function to work as expected in theWordListcomponent.Undefined Errors During Initial Render: useEffect() and find()
Initial Rendering:
users = []).user.namewhenuserisundefined, causing an error.Execution of
useEffect:useEffectruns after the initial render. React ensures the DOM is updated before running the effect.fetchUsers()function is called insideuseEffectto fetch the data.State Update:
setUsers(data)updates the state, triggering a re-render of the component.Rendering with Data:
users.find()now finds the correct user and renders their name.Main Issue
useEffectdoes not execute immediately after the component renders; it runs after the initial render. This can lead to unexpected behaviour and errors if not properly handled.Solution
To solve this error, implement a conditional check before rendering the content that depends on the data:
return ( <> {user ? <h1>{user.name}</h1> : <p>Loading...</p>} </> );This solution prevents the error on the first render by showing a loading message while the data is still being fetched.
Best Practices
useEffect: Identify all dependencies used inside theuseEffecthook and ensure they are included in the dependency array. This guarantees that the effect is re-executed when relevant values change.Alternative Approach Using Loading State:
const [isLoading, setIsLoading] = useState(true); useEffect(() => { const fetchData = async () => { try { const data = await fetchUsers(); setUsers(data); } finally { setIsLoading(false); } }; fetchData(); }, []); return ( <> {isLoading ? <p>Loading...</p> : user && <h1>{user.name}</h1>} </> );This approach provides better control over the loading state and avoids errors related to unavailable data during the initial render.
Future Improvements