How to change color of rectangle on resizing using React.JS?
Last Updated :
25 Jul, 2024
In web development, interactive UI elements can definitely enhance the user experience. One common task is altering an element's appearance based on user actions, such as resizing a window.
Prerequisites:
Approach: To change the color on resizing, we need to implement two features. First, we should be able to resize the rectangle. Second, we need to detect the size of the rectangle to update the color. Therefore, for resizing, we will use a re-resizable component for React and analyze the size by using the react-resize-detector library for React.
Steps to implement the above approach:
Step 1: Create a React application using the following command:
npx create-react-app demo
Step 2: After creating your project folder i.e. demo, move to it using the following command:
cd demo
Step 3: Install react-resize-detector and re-resizable from npm.
npm i react-resize-detector re-resizable
Open the src folder and delete the following files:
- logo.svg
- setupTests.js
- App.test.jsĀ
- index.css
- reportWebVitals.js
- App.css
Project Structure: The project should look like this:

Example: In this example, we'll see how to change the color of the rectangle on resizing in ReactJS
App.js
import React, { useState, useEffect } from 'react';
import { Resizable } from 're-resizable';
import { withResizeDetector } from 'react-resize-detector';
import React, { useState, useEffect } from 'react';
import { Resizable } from 're-resizable';
import { withResizeDetector } from 'react-resize-detector';
const App = ({ width, height }) => {
const [color, setColor] = useState('red');
const [state, setState] = useState({
width: 300, height: 100
});
useEffect(() => {
setColor(width > 200 ? width > 300 ?
'#dacbf7' : '#f1ccf8' : '#d3f8cc');
}, [width]);
return (
<>
<Resizable
style={{
backgroundColor: color,
margin: '20px',
border: "1px solid black",
color: 'green',
fontSize: '20px',
textAlign: 'center',
fontWeight: 'bold'
}}
size={{
width: state.width,
height: state.height
}}
onResizeStop={(d) => {
setState({
width: state.width + d.width,
height: state.height + d.height,
});
}}>
{`${width}x${height}`}
</Resizable>
<p style={{
color: 'blue', margin: '20px',
fontSize: '20px'
}}>Current Color: {color}</p>
</>
);
};
export default withResizeDetector(App);
Step to Run Application: Run the application using the following command from the root directory of the project:
npm start
Output: Now open your browser and go to http://localhost:3000/, you will see the following output:
Similar Reads
How to Change the Color of Icons using Material-UI in ReactJS? Changing the icon colors makes us able to keep the webpage according to themes. Material-UI icons is a React based module. React supports more than 1000 Material-UI icons. It is one of the most popular and in-demand frameworks.ApproachTo change the color of icons using the Material UI in React JS we
3 min read
How To Change Placeholder Color In ReactJS ? In this article, we'll explore two different approaches to changing the placeholder color in ReactJS. To change the color of the placeÂholder text, the CSS ::placÂeholder pseudo-element is primarily utilized. This handy pseudo-element enables us to style the placeÂholder text within an input field.
3 min read
How to change the navbar color when you scroll in ReactJS ? On scroll navbar color change in React highlights a sticky responsive navigation bar to navigate through web application pages. It provides an efficient way to navigate multiple pages in a single-page application. The following approach covers how to change the navbar color when you scroll through t
4 min read
How to create a rectangle canvas using Fabric.js ? In this article, we are going to create a canvas rectangle using FabricJS. The canvas rectangle means rectangle is movable and can be stretched according to requirement. Further, the rectangle can be customized when it comes to initial stroke color, height, width, fill color or stroke width. Approac
2 min read
Using the useRef hook to change an element's style in React In this article, we will learn how to change an element's style using the useRef hook. UseRef() allows us to create a reference to a DOM element and keep track of variables without causing re-renders. Prerequisites:NPM & Node JSReact JSReact useRef HookTo use a ref to change an element's style
3 min read
RGB Color Slider Using React js In this article, we will create a RGB color slider Using ReactJS. The RGB Color Slider web application offers an interactive way for users to choose and preview colors via red, green, and blue sliders. Among its features, it sports a real-time visualization of the selected color, as well as allows u
4 min read