React Interview Questions

Created
Aug 22, 2024 11:35 AM
Tags

Fundamentals

React Application Lifecycle

1. Mounting Phase
  • constructor(props): Initializes state and binds methods.
  • static getDerivedStateFromProps(nextProps, prevState): Updates state based on props.
  • render(): Returns JSX to render.
  • componentDidMount(): Invoked after the component mounts; ideal for API calls.
2. Updating Phase
  • static getDerivedStateFromProps(nextProps, prevState): Updates state before rendering.
  • shouldComponentUpdate(nextProps, nextState): Decides if re-rendering is necessary.
  • render(): Renders the updated component.
  • getSnapshotBeforeUpdate(prevProps, prevState): Captures information before updates.
  • componentDidUpdate(prevProps, prevState, snapshot): Invoked after updates; useful for side effects.
3. Unmounting Phase
  • componentWillUnmount(): Cleans up resources before the component is removed from the DOM.
notion image

QnA

What’s the difference b/w IServiceCollection vs IServiceProvider?

IServiceProvider
  • Responsible for resolving services and creating instances of registered types
  • Provides methods like GetService()GetRequiredService(), and GetServices() to retrieve service instances
  • Can be injected into classes as a constructor parameter to allow resolving dependencies
  • Should be used sparingly to avoid the Service Locator anti-pattern
  • Useful in cases where you need to dynamically create scoped instances, like in multithreaded apps
IServiceCollection
  • Used to register services and configure their lifetime (transient, scoped, singleton)
  • Cannot be injected into class constructors directly
  • Provides extension methods like AddTransient()AddScoped()AddSingleton() to register services
  • Ultimately used to build an IServiceProvider instance via BuildServiceProvider()