Exciting times lie ahead! In this lesson, we'll be mastering the concept of dynamic rendering in React. You're about to discover how to fetch data from an API and use it to create highly responsive applications. By the end, you'll be capable of implementing a search input that fetches and displays data according to user input.
Dynamic rendering is an approach providing real-time updates to UI elements based on data changes. Picture a weather forecast app that fetches real-time temperature data and updates it on the screen – that's our goal today. In React, dynamic rendering is achieved by updating a component's state
and making corresponding adjustments in the UI.
Let's utilize the Fetch API
to fetch some data from an API endpoint:
JavaScript1fetch('https://api-regional.codesignalcontent.com/posting-application-2/posts/') 2 .then(response => response.json()) 3 .then(data => console.log(data)) 4 .catch(error => console.error('Error:', error));
The code above prints data from the API as JSON, effectively pulling up new data from the API.
Our focus now shifts to a real-world scenario. Visualize creating an input field where users type in a user ID. When the 'Search' button is clicked, the app fetches the user's corresponding posts, stores them in the state, and then renders this new data.
Here's a React component to illustrate this concept:
JavaScript1function PostsSearch() { 2 const [inputValue, setInputValue] = useState(''); 3 const [posts, setPosts] = useState([]); 4 5 const fetchPosts = () => { 6 fetch(`https://api-regional.codesignalcontent.com/posting-application-2/posts/${inputValue}`) 7 .then(response => { 8 if (!response.ok) { 9 throw Error(response.statusText); 10 } 11 return response; 12 }) 13 .then(response => response.json()) 14 .then(data => setPosts(data)) 15 .catch(error => console.error('Error:', error)); 16 } 17 18 return (<div> 19 <input type="text" value={inputValue} onChange={e => setInputValue(e.target.value)} /> 20 <button onClick={fetchPosts}>Search</button> 21 <ul> 22 {posts.length > 0 ? (posts.map(post => <li key={post.id}>{post.title}</li>)) : (<li>No posts found.</li>)} 23 </ul> 24 </div>) 25} 26export default PostsSearch;
It's important to handle both the cases: when there is useful data returned from the API and when there is no data (e.g. when an invalid ID is inputted). In our case, we display a "No posts found." message when no posts are returned.
Congratulations! You have successfully mastered dynamic rendering in React. You've learned how to efficiently fetch API data and dynamically update the application state based on the fetched data.
Are you ready to put your knowledge into action? Coming up next, take on relevant exercises to solidify your understanding of these concepts. Keep practicing, and before long, you'll be creating magic with React!