title | description |
---|---|
ReactJs Interview Question 2023 |
Find the top React job interview questions for 2023 for beginners, frontend developers, junior developers as well as for experienced developers which might help you cracking your next interview. |
Updated Dec 29, 2022
Here you'll find the top React job interview questions for 2023 for beginners, frontend developers, junior developers as well as for experienced developers which might help you cracking your next interview.
ReactJs is a popular JavaScript library for building user interfaces. It is maintained by Facebook, and is widely used for building web applications, mobile apps, and other user interfaces. React allows developers to create reusable components, which can help make large applications easier to manage and maintain. It is designed to be efficient, declarative, and flexible, and can be used to create complex, dynamic user interfaces.
*Discover the answers by clicking on the questions.
React creates a virtual DOM. When the state changes in a component it first runs a "diffing" algorithm, which identifies what has changed in the virtual DOM. The second step is reconciliation, where it updates the DOM with the results of diff.
- It is easy to know how a component is rendered, you just need to look at the render function.
- JSX makes it easy to read the code of your components. It is also really easy to see the layout, or how components are plugged/combined.
- You can render React on the server side. This improves SEO and performance.
- It is easy to test.
- You can use React with any framework you wish as it is only a view layer.
Presentational components are concerned with how things look. They generally receive data and callbacks exclusively via props. These components rarely have their own state, but when they do it generally concerns UI state, as opposed to data state.
When your component just receives props and renders them to the page, this is a stateless component
, for which a pure function can be used. These are also called dumb components or presentational components.
Container components are more concerned with how things work. These components provide the data and behavior to presentational or other container components. They define actions and provide these as callbacks to the presentational components. They are also often stateful as they serve as data sources.
-
The class component uses ES6 class syntax, and it extends React components with a render method that returns React elements.
-
Functional components with hooks are purely JavaScript functions that also return React elements. Before the introduction of hooks, functional components were stateless.
State is a data structure that starts with a default value when a Component mounts. It may be mutated across time, mostly as a result of user events.
Props (short for properties) are a Component's configuration. They are received from above and immutable as far as the Component receiving them is concerned. A Component cannot change its props, but it is responsible for putting together the props of its child Components. Callback functions can also be passed in as props.
componentWillMount
(deprecated) - this is most commonly used for App configuration in your root component.componentDidMount
- here you want to do all the setup you couldn’t do without a DOM, and start getting all the data you need. Also if you want to set up eventListeners etc. this lifecycle hook is a good place to do that.componentWillReceiveProps
(deprecated) - this lifecyclye acts on particular prop changes to trigger state transitions.shouldComponentUpdate
- if you’re worried about wasted renders shouldComponentUpdate is a great place to improve performance as it allows you to prevent a rerender if component receives new prop. shouldComponentUpdate should always return a boolean and based on what this is will determine if the component is rerendered or not.componentWillUpdate
(deprecated) - rarely used. It can be used instead of componentWillReceiveProps on a component that also has shouldComponentUpdate (but no access to previous props).componentDidUpdate
- also commonly used to update the DOM in response to prop or state changes.componentWillUnmount
- enables you can cancel any outgoing network requests, or remove all event listeners associated with the component.
Hooks let you use more of React’s features without having to use classes. The first hook that you will most likely encounter is useState. useState is a Hook that lets you add React state to function components. It returns an array with a getter and a setter.
The syntax looks like
const [count, setCount] = React.useState(0);
<button onClick={() => setCount(count + 1)}>Increase Count</button>;
The equivalent when using a class component would be.
this.state = {
count: 0,
};
<button onClick={() => this.setState({ count: this.state.count + 1 })}>
Increase Count
</button>;
The next hook you will most likely encounter is useEffect. The Effect Hook lets you perform side effects in function components. By passing an empty array as the second argument to useEffect is equivalent to using componentDidMount. If you pass a value to the array it will only call the useEffect function when the value in the array updates.
useEffect(() => {
// do stuff when the component mounts
}, []);
componentDidMount
is where an AJAX request should be made in a React component. This method will be executed when the component mounts
(is added to the DOM) for the first time. This method is only executed once during the component’s life. Importantly, you can’t guarantee the AJAX request will have resolved before the component mounts. If it doesn't, that would mean that you’d be trying to setState on an unmounted component, which would not work. Making your AJAX request in componentDidMount
will guarantee that there is a component to update.
In HTML, form elements such as <input>
, <textarea>
, and <select>
typically maintain their own state and update it based on user input. When a user submits a form the values from the mentioned elements are sent with the form. With React it works differently. The component containing the form will keep track of the value of the input in it's state and will re-render the component each time the callback function e.g. onChange is fired as the state will be updated. An input form element whose value is controlled by React in this way is called a controlled component
.
Refs are used to get reference to a DOM node or an instance of a component in React. Good examples of when to use refs are for managing focus/text selection, triggering imperative animations, or integrating with third-party DOM libraries. You should avoid using string refs and inline ref callbacks. Callback refs are advised by React.
A higher-order component is a function that takes a component and returns a new component. HOC's allow you to reuse code, logic and bootstrap abstraction. The most common is probably Redux’s connect function. Beyond simply sharing utility libraries and simple composition, HOCs are the best way to share behavior between React Components. If you find yourself writing a lot of code in different places that does the same thing, you may be able to refactor that code into a reusable HOC.
- Scope safety: Until arrow functions, every new function defined its own this value (a new object in the case of a constructor, undefined in strict mode function calls, the base object if the function is called as an "object method", etc.). An arrow function does not create its own this, the this value of the enclosing execution context is used.
- Compactness: Arrow functions are easier to read and write.
- Clarity: When almost everything is an arrow function, any regular function immediately sticks out for defining the scope. A developer can always look up the next-higher function statement to see what the Object is.
Returning null from a component's render method means nothing will be displayed, but it does not affect the firing of the component's lifecycle methods.
If the amount of times the component re-renders is an issue, there are two options available. Manually implementing a check in the shouldComponentUpdate
lifecycle method hook.
shouldComponentUpdate(nextProps, nextState){
const allowRender = true;
// Do some check here and assign decicison to allowRender
return allowRender
}
Or using React.PureComponent instead of React.Component React.PureComponent implements shouldComponentUpdate() with a shallow prop and state comparison. This enables you to avoid re-rendering the component with the same props and state.
Keys help React identify which items have changed, are added, or are removed. Keys should be given to the elements inside the array to give the elements a stable identity. The best way to pick a key is to use a string that uniquely identifies a list item among its siblings. Most often you would use IDs from your data as keys. When you don't have stable IDs for rendered items, you may use the item index as a key as a last resort. It is not recommend to use indexes for keys if the items can reorder, as that would be slow.
A child class constructor cannot make use of this until super()
has been called. Also, ES2015 class constructors have to call super()
if they are subclasses. The reason for passing props to super()
is to enable you to access this.props
in the constructor.
- JSX is a syntax extension to JavaScript and comes with the full power of JavaScript. JSX produces React
elements
. - You can embed any JavaScript expression in JSX by wrapping it in curly braces. After compilation, JSX expressions become regular JavaScript objects.
- This means that you can use JSX inside of
if
statements andfor loops
, assign it to variables, accept it as arguments, and return it from functions.
const element = <h1 className="greeting">Hello, world!</h1>;
const element = React.createElement(
"h1",
{ className: "greeting" },
"Hello, world!"
);
- The basic idea of redux is that the entire application state is kept in a single store. The store is simply a javascript object.
- The only way to change the state is by sending actions from your application and then writing reducers for these actions that modify the state.
- The entire state transition is kept inside reducers and should not have any
side-effects
.
The store is a javascript object that holds application state. Along with this it also has the following responsibilities:
- Allows access to state via
getState();
. - Allows state to be updated via
dispatch(action);
. - Registers listeners via
subscribe(listener);
. - Handles unregistering of listeners via the function returned by
subscribe(listener)
.
- Actions are plain javascript objects.
- They must have a type indicating the type of action being performed.
- In essence, actions are payloads of information that send data from your application to your store.
A reducer is simply a pure function that takes the previous state and an action, and returns the next state.
- Redux thunk is middleware that allows you to write action creators that return a function instead of an action.
- The thunk can then be used to delay the dispatch of an action if a certain condition is met. This allows you to handle the asynchronous dispatching of actions.
//hook
const useDebounce = (value, delay) => {
const [debouncedValue, setDebouncedValue] = useState(value);
useEffect(() => {
const timeout = setTimeout(() => {
setDebouncedValue(value);
}, delay);
return () => {
clearTimeout(timeout);
};
}, [value]);
return debouncedValue;
};
//example
const Counter = () => {
const [value, setValue] = useState(0);
const lastValue = useDebounce(value, 1000);
return (
<div>
<p>
Current Value: {value} | Debounced Value: {lastValue}
</p>
<button onClick={() => setValue(value + 1)}>Increment</button>
</div>
);
};
// hook
function useCopyToClipboard(content) {
const [isCopied, setIsCopied] = useState(false);
const copy = useCallback(() => {
navigator.clipboard
.writeText(content)
.then(() => setIsCopied(true))
.then(() => setTimeout(() => setIsCopied(false), 1250))
.catch((err) => alert(err));
}, [content]);
return [isCopied, copy];
}
// usage
export default function App() {
const [isCopied, copy] = useCopyToClipboard("Text to copy!");
return <button onClick={copy}>{isCopied ? "Copied!" : "Copy"}</button>;
}
-
useId does not take any parameters.
-
useId returns a unique ID string associated with this particular useId call in this particular component.
//usage
import { useId } from "react";
const App = () => {
const id = useId();
return (
<form>
<label htmlFor={`email-${id}`}>Email</label>
<input type="text" id={`email-${id}`} name="email" />
<label htmlFor={`password-${id}`}>Password</label>
<input type="password" id={`password-${id}`} name="password" />
</form>
);
};
// 🔴 Bad Practise - Don't use for key
const id = useId();
return posts.map((post) => <article key={id}>...</article>);
-
We can use 'prop-types' package
-
Earlier, till React v15.5 this was there as part of React iteslf
import PropTypes from "prop-types";
function MyComponent({ name }) {
return <div>Hello, {name}</div>;
}
MyComponent.propTypes = {
name: PropTypes.string,
};
export default MyComponent;
- Show a loader while a component waits for data
//HOC
function WithLoading(Component) {
return function WihLoadingComponent({ isLoading, ...props }) {
if (!isLoading) return <Component {...props} />;
return <p>Please wait, fetching your data in no time...</p>;
};
}
export default WithLoading;
//usage
import UserListComponent from "./UserListComponent.js"; //importing component
import WithLoading from "./withLoading.js"; //importing HOC
const ListWithLoading = WithLoading(UserListComponent); //connect component with HOC
const App = () => {
const [loading, setLoading] = useState(true);
const [users, setUsers] = useState([]);
useEffect(() => {
//fetch data
const dataFromApi = ["this is coming from API call", "don't show loader"];
//at this time loader will be shown in the UI using HOC
//data fetched successfully
setUsers([...dataFromApi]);
setLoading(false);
}, []);
return <ListWithLoading isLoading={loading} users={users} />;
};
-
'useDeferredValue' is a React Hook that lets you defer updating a part of the UI.
-
Basically it let you perform the debouncing technique with lesser code.
//usage
import { useState, useDeferredValue } from "react";
//userList component takes searchText to fetch user's list
import UserList from "./UserList.js";
export default function App() {
const [searchText, setSearchText] = useState("");
//pass searchText as default visible value in useDeferredValue
const deferredQuery = useDeferredValue(searchText);
return (
<>
<label>
Search user:
<input
value={searchText}
onChange={(e) => setSearchText(e.target.value)}
/>
</label>
<div>
<UserList searchText={deferredQuery} />
</div>
</>
);
}
export default function OutsideAlerter() {
const clickMeDivRef = useRef(null);
useEffect(() => {
const handleClickOutside = (event) => {
if (!ref?.current?.contains(event.target)) {
alert("You clicked outside of me!");
}
};
// Bind the event listener
document.addEventListener("mousedown", handleClickOutside);
return () => {
// Unbind the event listener on clean up
document.removeEventListener("mousedown", handleClickOutside);
};
}, [clickMeDivRef]);
return <div ref={clickMeDivRef}>Clicked me?</div>;
}
In JSX, lowercase tag names are considered to be HTML tags. However, lowercase tag names with a dot (property accessor) aren't.
<person />
compiles to React.createElement('person') (html tag)<Person />
compiles to React.createElement(Person)<obj.person />
compiles to React.createElement(obj.person)
// Wrong! This is a component and should be in uppercase.
function person(props) {
// Correct! This usage of <div> is correct because div is a valid element.
return <div>{props.isLearning ? "Great!" : "Call Mom!"}</div>;
}
function App() {
// Wrong! React thinks <person /> is a HTML tag because it's not capitalized.
return <person isLearning={true} />;
}
// Correct! This is a component and should be capitalized
function Person(props) {
// Correct! This usage of <div> is correct because div is a valid element.
return <div>{props.isLearning ? "Great!" : "Call Mom!"}</div>;
}
function App() {
// Correct! React knows <Person /> is a component because it's capitalized.
return <Person isLearning={true} />;
}
- NPM is a package manager and can be used to install node.js packages.
- NPX is a tool to execute node.js packages.
It doesn't matter whether you installed that package globally or locally. NPX will temporarily install it and run it. NPM also can run packages if you configure a package.json file.
So if you want to check/run a node package quickly without installing it - use NPX.
'create-react-app' is a npm package that is expected to be run only once in a project's lifecycle. Hence, it is preferred to use npx to install and run it in a single step.
> npx create-react-app codinn
npM - Manager
npX - Execute
import React, { useEffect, useRef } from "react";
const SearchPage = () => {
const textInput = useRef(null);
useEffect(() => {
textInput.current.focus();
}, []);
return (
<div>
<input ref={textInput} type="text" />
</div>
);
};
//old - v5
import { useHistory } from "react-router-dom";
function HomeButton() {
let history = useHistory();
history.push('/some/path') here
};
//new - v6+
import { useNavigate } from "react-router-dom";
function SignupForm() {
let navigate = useNavigate();
async function handleSubmit(event) {
event.preventDefault();
await submitForm(event.target);
navigate("../success", { replace: true });
}
return <form onSubmit={handleSubmit}>{/* ... */}</form>;
}
//or
import { redirect } from "react-router-dom";
const loader = async () => {
const user = await getUser();
if (!user) {
return redirect("/login");
}
};
Given Snippet
export default function Counter() {
const [number, setNumber] = useState(0);
return (
<>
<h1>{number}</h1>
<button
onClick={() => {
setNumber(number + 1);
setNumber(number + 1);
setNumber(number + 1);
}}
>
+3
</button>
</>
);
}
Output
- on click of '+3' -> prints '1'
- or update state only once because of state batching concept
Why?
This lets you update multiple state variables without triggering too many re-renders.
But if you want to update anyways? That is - it need to print 3 on click of '+3'.
Pass the callback method to setNumber
.
setNumber((n) => n + 1);
return (
<>
<h1>{number}</h1>
<button
onClick={() => {
setNumber((n) => n + 1);
setNumber((n) => n + 1);
setNumber((n) => n + 1);
}}
>
+3
</button>
</>
);
Passing data between sibling components of React is possible using React Router useParams
hook.
Parent component (usually App.js to define routes)
<Route path="/user/:id" element={<User />} />
import { useParams } from "react-router-dom";
const User = () => {
let { id } = useParams();
useEffect(() => {
console.log(`/user/${id}`);
}, []);
// .....
};
//1. create context
const GlobalLanguageContext = React.createContext(null);
const App = () => {
const contextValue = { language: "EN" };
return (
//2. connect with all the child components under Provider
//One time Config - Here in Provider's value prop you can pass
//the value of your context global variable
<GlobalLanguageContext.Provider value={contextValue}>
<Child />
</GlobalLanguageContext.Provider>
);
};
const Child = () => {
//3. use variable
const { language } = React.useContext(GlobalLanguageContext);
return <div>Application Language: {language}</div>;
};
- useCallback gives you referential equality between renders for functions. And useMemo gives you referential equality between renders for values.
- useCallback and useMemo both expect a function and an array of dependencies. The difference is that useCallback returns its function when the dependencies change while useMemo calls its function and returns the result.
- useCallback returns its function uncalled so you can call it later, while useMemo calls its function and returns the result
- Create React App (CRA) has long been the go-to tool for most developers to scaffold React projects and set up a dev server. It offers a modern build setup with no configuration.
- But, we see increased development and build time when the project size increases. This slow feedback loop affects developer's productivity and happiness.
- To address these issues, there is a new front-end tooling in the ecosystem:
Vite
. - Unlike CRA, Vite does not build your entire application before serving, instead, it builds the application on demand. It also leverages the power of native ES modules, esbuild, and Rollup to improve development and build time.
- Vite is a next-generation, front-end tool that focuses on speed and performance.
- Vite is a development server that provides rich feature enhancements over native ES modules: fast Hot Module Replacement (HMR), pre-bundling, support for typescript, jsx, and dynamic import.
- A build command that bundles your code with Rollup, pre-configured to output optimized static assets for production.
- The major advantage of
react-router
is that the page does not have to be refreshed when a link to another page is clicked. - It also allows us to use browser's
history
feature while preserving the right application view. - Better user experience, animations and transitions can be easily implemented when switching between different components.
- React Router uses
dynamic routing
to ensure that routing is achieved as it is requested by the user. This also means that all the required components are also rendered without any flashes of white screen or page reload. - The main components of
react-router
are:BrowserRouter
,Routes
,Route
,Link
.
- One way is to use the shouldComponentUpdate lifecycle method to prevent unnecessary re-renders of a component.
- Another way is to use the PureComponent class, which implements shouldComponentUpdate with a shallow comparison of props and state.
- Additionally, using the React.memo higher-order component can optimize the performance of functional components.
To implement CRUD (create, read, update, delete) functionality in a React application using hooks, you can use the useState hook to manage the state of your application and the useEffect hook to handle side effects, such as making API calls to a server to create, read, update, or delete data.
Here is an example of how you might implement CRUD functionality in a React component using hooks:
import React, { useState, useEffect } from "react";
function App() {
// useState hook to manage the state of our items
const [items, setItems] = useState([]);
// useEffect hook to fetch the items from an API
useEffect(() => {
fetch("https://my-api.com/items")
.then((response) => response.json())
.then((data) => setItems(data));
}, []);
// helper function to add a new item
const addItem = (name) => {
const newItem = { name };
setItems([...items, newItem]);
};
// helper function to update an item
const updateItem = (index, name) => {
const updatedItems = [...items];
updatedItems[index] = { name };
setItems(updatedItems);
};
// helper function to delete an item
const deleteItem = (index) => {
const updatedItems = [...items];
updatedItems.splice(index, 1);
setItems(updatedItems);
};
// render the items in a list
return (
<ul>
{items.map((item, index) => (
<li key={index}>
{item.name}
<button onClick={() => updateItem(index, "updated name")}>
Update
</button>
<button onClick={() => deleteItem(index)}>Delete</button>
</li>
))}
<button onClick={() => addItem("new item")}>Add item</button>
</ul>
);
}
A hook in React is a function that allows developers to use state and other React features without writing a class. This makes it possible to use these features in functional components, which can be easier to write and understand than class-based components.
Some common hooks that are used in React include useState, useEffect, and useContext. The useState hook allows a functional component to have local state, the useEffect hook allows a functional component to perform side effects, and the useContext hook allows a functional component to access values from the nearest context provider.
No, hooks can only be used inside functional components. If you need to use state or other React features in a class-based component, you will need to use a class component.
You can test a component that uses hooks by using the act utility from the react-testing-library package. This utility allows you to simulate the effects of React's reconciliation process, which is necessary for hooks to work correctly. You can then use standard Jest or Enzyme assertions to verify the behavior of your component.
The useEffect hook is used for performing side effects in functional components. This can include things like data fetching, setting up subscriptions, or manually changing the DOM. The useEffect hook is called after the component renders, and can be used to ensure that your component stays up-to-date with any relevant data or dependencies.
To create a custom hook in React, you can use the useState hook to add local state to a functional component. Here's an example:
import { useState } from "react";
function useCounter() {
const [count, setCount] = useState(0);
function increment() {
setCount(count + 1);
}
return { count, increment };
}
This hook adds a count state and an increment function to a component. To use this hook in a component, you can call it at the top of the component function, like this:
function MyComponent() {
const { count, increment } = useCounter();
return (
<div>
<p>The count is {count}.</p>
<button onClick={increment}>Increment</button>
</div>
);
}
Now, whenever the increment button is clicked, the count state will be updated and the component will re-render with the new value.
Here is an example of how you might use useEffect and useLayoutEffect in a React component:
import React, { useState, useEffect, useLayoutEffect } from "react";
function MyComponent() {
const [count, setCount] = useState(0);
// useEffect runs after the render cycle has completed
useEffect(() => {
// This code will run every time the component renders,
// after the render is complete.
console.log("useEffect running");
});
// useLayoutEffect runs synchronously immediately after the render cycle
useLayoutEffect(() => {
// This code will run every time the component renders,
// before the browser has a chance to paint the update to the screen.
// Be careful! This can cause visual inconsistencies.
console.log("useLayoutEffect running");
});
return (
<div>
<p>Count: {count}</p>
<button onClick={() => setCount(count + 1)}>Increment</button>
</div>
);
}
In this example, when the Increment button is clicked, the useEffect hook will run after the component has been updated and re-rendered, whereas the useLayoutEffect hook will run before the update is painted to the screen. This means that if you were to use useLayoutEffect to update the UI, the user might see the UI update before the update is complete, which can cause visual inconsistencies. useEffect, on the other hand, runs after the update is complete and is therefore safer to use for updating the UI.
-
The virtual DOM is faster to update than the real DOM because React uses a clever technique to minimize the number of updates that need to be made to the real DOM.
-
When you update the virtual DOM, React will compare the new virtual DOM with the old one, determine which parts have changed, and then update the real DOM accordingly. This means that only the parts of the DOM that actually need to be changed are updated, which is much faster than updating the entire DOM every time there is a change.
-
Furthermore, the virtual DOM is implemented in JavaScript, which is generally faster to execute than the native code that is used to manipulate the real DOM.
-
This means that React can perform updates to the virtual DOM quickly, and then use the resulting diff to make efficient updates to the real DOM.
Overall, the use of the virtual DOM allows React to make efficient updates to the UI, which results in a faster and more responsive user experience.
In React, a pure function is a function that returns the same output for the same set of inputs, regardless of when it is called. An impure function, on the other hand, is a function that may produce different outputs for the same set of inputs, depending on when it is called or other factors.
Here is an example of a pure function in React:
function addNumbers(a, b) {
return a + b;
}
This function takes in two numbers, a and b, and returns their sum. This function will always return the same result for the same input, regardless of when it is called or what state the component is in.
Here is an example of an impure function in React:
function getRandomNumber() {
return Math.random();
}
This function returns a random number every time it is called. Because the output of this function depends on factors outside of its control (in this case, the current time and a random seed), it is considered an impure function.
In general, pure functions are preferred in React because they are easier to reason about and test. Impure functions, on the other hand, can introduce unpredictable behavior and make your code more difficult to understand.
Styled Components is a library for React and React Native that allows you to write actual CSS code to style your components. It allows you to write your styles in a declarative way alongside your components, rather than having to maintain separate style sheets.
Here is an example of using Styled Components in a React component:
import styled from "styled-components";
const Button = styled.button`
background: palevioletred;
border-radius: 3px;
border: none;
color: white;
`;
function MyComponent() {
return <Button>Click me!</Button>;
}
In this example, the Button component is styled with a pale violet red background and white text. The styles are written within a template literal and are applied to the button element. When the Button component is rendered, it will have these styles applied to it.
Styled Components allows you to easily customize your styles based on props passed to the component. For example:
const Button = styled.button`
background: ${(props) => (props.primary ? "palevioletred" : "white")};
border-radius: 3px;
border: none;
color: ${(props) => (props.primary ? "white" : "palevioletred")};
`;
function MyComponent() {
return (
<div>
<Button>Click me!</Button>
<Button primary>Click me!</Button>
</div>
);
}
In this example, the Button component has a customizable background and text color based on the primary prop. The first Button will have a white background and pale violet red text, while the second Button will have a pale violet red background and white text.
It really depends on your specific needs and preferences. Both inline styling and Styled Components have their own advantages and disadvantages, and the best choice for you will depend on the requirements of your project.
Inline styling refers to the practice of applying styles directly to elements using the style attribute. In React, this can be done using the style prop on elements. For example:
function MyComponent() {
return <div style={{ color: "red", fontSize: "20px" }}>Hello, World!</div>;
}
One advantage of inline styling is that it can be very simple to use and understand. There's no need to import additional libraries or set up complex configurations. Inline styling also allows you to easily apply styles based on props or state, which can be very useful in certain situations.
However, inline styling can also have some drawbacks. It can make your code more cluttered and harder to read, especially for complex styles. It can also be more difficult to reuse styles across different components, as you would need to copy and paste the style objects between components.
Styled Components is a library that allows you to define styles using actual CSS syntax and apply them to React components. It allows you to write your styles in a declarative way alongside your components, rather than having to maintain separate style sheets. Here's an example of using Styled Components in a React component:
import styled from "styled-components";
const Button = styled.button`
background: palevioletred;
border-radius: 3px;
border: none;
color: white;
`;
function MyComponent() {
return <Button>Click me!</Button>;
}
One advantage of Styled Components is that it helps to keep your styles organized and modular. Instead of having a separate CSS file for each component, you can define the styles directly within the component itself. This can make it easier to understand and maintain your code, as everything related to the component is kept in one place.
Styled Components also allows you to easily customize your styles based on props passed to the component, and to define complex styles using standard CSS syntax.
However, Styled Components does require an additional library to be installed and imported, which can add some complexity to your project. It may also have a slightly higher learning curve for developers who are not familiar with CSS-in-JS libraries.
Ultimately, the choice between inline styling and Styled Components will depend on your specific needs and preferences. If you're looking for a quick and easy way to apply simple styles, inline styling may be the way to go. If you want more control and flexibility over your styles, and are willing to invest some time in learning a new library, Styled Components may be a better choice.
function App() {
const [count, setCount] = React.useState(0);
return (
<div>
<p>You clicked {count} times</p>
<button onClick={() => setCount(count + 1)}>Click me</button>
</div>
);
}
function App() {
const [count, setCount] = React.useState(0);
return (
<div>
<p>You clicked {count} times</p>
<button onClick={() => setCount(count + 1)}>Click me</button>
</div>
);
}
The output would be "You clicked 2 times".
54. What is the output of the following code snippet when the "Increment age" button is clicked three times?
function App() {
const [state, setState] = React.useState({
name: "John",
age: 30,
});
return (
<div>
<p>
My name is {state.name} and I am {state.age} years old
</p>
<button onClick={() => setState({ ...state, age: state.age + 1 })}>
Increment age
</button>
</div>
);
}
54. What is the output of the following code snippet when the "Increment age" button is clicked three times?
function App() {
const [state, setState] = React.useState({
name: "John",
age: 30,
});
return (
<div>
<p>
My name is {state.name} and I am {state.age} years old
</p>
<button onClick={() => setState({ ...state, age: state.age + 1 })}>
Increment age
</button>
</div>
);
}
The output would be "My name is John and I am 33 years old".
55. What is the output of the following code snippet when the "Add hobby" button is clicked twice and then the page is refreshed?
function App() {
const [state, setState] = React.useState({
name: "John",
age: 30,
hobbies: ["reading", "running"],
});
return (
<div>
<p>
My name is {state.name} and I am {state.age} years old
</p>
<ul>
{state.hobbies.map((hobby) => (
<li key={hobby}>{hobby}</li>
))}
</ul>
<button
onClick={() =>
setState({ ...state, hobbies: [...state.hobbies, "swimming"] })
}
>
Add hobby
</button>
</div>
);
}
55. What is the output of the following code snippet when the "Add hobby" button is clicked twice and then the page is refreshed?
function App() {
const [state, setState] = React.useState({
name: "John",
age: 30,
hobbies: ["reading", "running"],
});
return (
<div>
<p>
My name is {state.name} and I am {state.age} years old
</p>
<ul>
{state.hobbies.map((hobby) => (
<li key={hobby}>{hobby}</li>
))}
</ul>
<button
onClick={() =>
setState({ ...state, hobbies: [...state.hobbies, "swimming"] })
}
>
Add hobby
</button>
</div>
);
}
The output would be a list with two items: "reading" and "running". The state of the component is reset when the page is refreshed, so the hobbies list would only contain the original two items after the refresh.