React js interview Question and Answer
Q 1 What is difference between class component & function component
Syntax: Class components are defined using the class keyword, while function components are defined using a JavaScript function.
State: Class components can have state, which allows them to keep track of data that changes over time. Function components, on the other hand, cannot have state until the introduction of React hooks.
Lifecycle methods: Class components have lifecycle methods, such as componentDidMount and componentWillUnmount, which allow developers to execute code at specific points in the component's lifecycle. Function components did not have these until the introduction of React hooks.
Code organization: Class components tend to have more verbose syntax and can be harder to read and follow. Function components, especially with hooks, can be simpler and more concise, making them easier to read and maintain.
Performance: Function components are generally faster and have better performance than class components because they don't have the overhead of the additional class hierarchy and instance creation.
Overall, while class components have been the traditional way of defining React components, function components are becoming more popular due to their simplicity and better performance. With the introduction of React hooks, function components now have the ability to manage state and have access to lifecycle methods, making them even more powerful.
Q 2 What is Styled Component ?
Styled Components is a library for React and React Native that allows you to write CSS code in your JavaScript code. It provides a more modular and component-based approach to styling, making your code more maintainable and reusable.
Q 3 what is Hooks? Types of Hooks? All hooks exxplain ?
State hooks: useState is the most commonly used state hook. It allows you to manage state in functional components. It returns an array with two elements: the current state value and a function to update that value.
Effect hooks: useEffect is the most commonly used effect hook. It allows you to perform side effects in functional components. It runs after every render, and you can use it to fetch data, update the DOM, or subscribe to events.
Context hooks: useContext allows you to access a React context from within a functional component. It takes a context object as an argument and returns the current value of that context.
Ref hooks: useRef allows you to create a ref object that can be used to reference a DOM element or a component instance.
Callback hooks: useCallback allows you to create a memoized callback function that can be used in child components without triggering unnecessary re-renders.
Memo hooks: useMemo allows you to memoize the result of a computation and reuse it across renders.
Custom hooks: Custom hooks are functions that allow you to encapsulate reusable logic in a way that can be shared across components. You can use them to abstract away complex logic and make your code more modular and reusable.
Overall, hooks are a powerful feature in React that allow you to write more expressive and reusable code in functional components.
Q 4 React Life Cycle Method Explain in both function comp. & clas comp. or useEffect se
Q 5 What is State Variable & Local Variable ?
State Variable - In React, a state variable is a variable that is declared and managed by a React component. State variables are used to store and manage component-specific data that can change over time. When a state variable changes, React automatically updates the component to reflect the new state.
You can declare a state variable in a functional component using the useState hook, which takes an initial state value and returns an array containing the current state value and a function to update the state:
In React, you can use the useMemo hook to memoize a function. Here's an example of how to use useMemo to memoize a function in a React component:
In this case, the memoized function performs an expensive computation with the prop1 and prop2 props. By passing [prop1, prop2] as the second argument to useMemo, the memoized function will only be recomputed if prop1 or prop2 changes.
Comments
Post a Comment