30 React Basic Exercises for Advanced with Solutions

Master advanced React skills with our comprehensive list of top 30 exercises. Dive into coding challenges that improve your understanding and proficiency in React, setting a solid foundation for professional-level challenges. Start your journey to React mastery today!

Learning Objectives:

These advanced React exercises focus on performance optimization, server-side rendering, advanced state management with Redux, and integrating APIs. By completing these challenges, you'll gain expertise in building high-performance, scalable, and maintainable React applications.

Exercise Instructions:

  • Start with the first exercise and attempt to solve it before checking the hint or solution.
  • Ensure you understand the logic behind each solution, as this will help you in more complex problems.
  • Use these exercises to reinforce your learning and identify areas that may require further study.

1. Create a component that finds the longest word in a hardcoded string and displays it.

Required Input:

The quick brown fox jumped over the lazy dog

Expected Output:

Longest Word: jumped

Code In React

import React from "react";
 
 const LongestWord = () => {
  return (
  <div>
  {/* Display the longest word */}
  </div>
  );
 };
 
 export default LongestWord;

Need help ?

Click on Hint to solve the question.

2. Build a component that tracks and displays the frequency of each letter in a hardcoded string.

Required Input:

hello

Expected Output:

Frequency: { h: 1, e: 1, l: 2, o: 1 }

Code In React

import React from "react";
 
 const LetterFrequency = () => {
  return (
  <div>
  {/* Display the letter frequencies */}
  </div>
  );
 };
 
 export default LetterFrequency;

Need help ?

Click on Hint to solve the question.

3. Write a component that calculates the Fibonacci sequence up to a given hardcoded number and displays the sequence.

Required Input:

7

Expected Output:

Fibonacci Sequence: [0, 1, 1, 2, 3, 5, 8]

Code In React

import React from "react";
 
 const FibonacciSequence = () => {
  return (
  <div>
  {/* Display the Fibonacci sequence */}
  </div>
  );
 };
 
 export default FibonacciSequence;

Need help ?

Click on Hint to solve the question.

4. Create a component that checks if a hardcoded string is a palindrome and displays the result (true or false).

Required Input:

madam

Expected Output:

Is Palindrome: true

Code In React

import React from "react";
 
 const CheckPalindrome = () => {
  return (
  <div>
  {/* Display whether the string is a palindrome */}
  </div>
  );
 };
 
 export default CheckPalindrome;

Need help ?

Click on Hint to solve the question.

5. Develop a component that merges two hardcoded arrays of objects based on a common property and displays the merged array.

Required Input:

[{ "id": 1, "name": "Alice" }, { "id": 2, "name": "Bob" }]
 [{ "id": 1, "age": 25 }, { "id": 2, "age": 30 }]

Expected Output:

Merged Array: [{ id: 1, name: 'Alice', age: 25 }, { id: 2, name: 'Bob', age: 30 }]

Code In React

import React from "react";
 
 const MergeArrays = () => {
  return (
  <div>
  {/* Display the merged array */}
  </div>
  );
 };
 
 export default MergeArrays;

Need help ?

Click on Hint to solve the question.

6. Write a component that finds the most common number in a hardcoded array and displays it.

Required Input:

[1, 2, 2, 3, 3, 3, 4]

Expected Output:

Most Common Number: 3

Code In React

import React from "react";
 
 const MostCommonNumber = () => {
  return (
  <div>
  {/* Display the most common number */}
  </div>
  );
 };
 
 export default MostCommonNumber;

Need help ?

Click on Hint to solve the question.

7. Create a component that takes a hardcoded JSON object and flattens it into a single-level object.

Required Input:

{ "a": 1, "b": { "c": 2, "d": { "e": 3 } } }

Expected Output:

Flattened Object: { "a": 1, "b.c": 2, "b.d.e": 3 }

Code In React

import React from "react";
 
 const FlattenObject = () => {
  return (
  <div>
  {/* Display the flattened object */}
  </div>
  );
 };
 
 export default FlattenObject;

Need help ?

Click on Hint to solve the question.

8. Build a component that generates a Pascal’s Triangle for a given hardcoded number of rows.

Required Input:

5 (Number of rows)

Expected Output:

Pascal's Triangle: [[1], [1, 1], [1, 2, 1], [1, 3, 3, 1], [1, 4, 6, 4, 1]]

Code In React

import React from "react";
 
 const PascalsTriangle = () => {
  return (
  <div>
  {/* Display Pascal's Triangle */}
  </div>
  );
 };
 
 export default PascalsTriangle;

Need help ?

Click on Hint to solve the question.

9. Write a component that removes duplicates from a hardcoded array and displays the unique elements.

Required Input:

[1, 2, 2, 3, 4, 4, 5]

Expected Output:

Unique Elements: [1, 2, 3, 4, 5]

Code In React

import React from "react";
 
 const RemoveDuplicates = () => {
  return (
  <div>
  {/* Display unique elements */}
  </div>
  );
 };
 
 export default RemoveDuplicates;

Need help ?

Click on Hint to solve the question.

10. Create a component that finds the maximum depth of a hardcoded nested array and displays the depth.

Required Input:

[1, [2, [3, [4]]]]

Expected Output:

Maximum Depth: 4

Code In React

import React from "react";
 
 const MaxDepth = () => {
  return (
  <div>
  {/* Display the maximum depth */}
  </div>
  );
 };
 
 export default MaxDepth;

Need help ?

Click on Hint to solve the question.

ad vertical

1 of 3