React Js
React Js
1. **What is React?**
- React is a JavaScript library for building user interfaces, developed by
Facebook. It allows you to build single-page applications with a component-based
architecture.
4. **What is JSX?**
- JSX (JavaScript XML) is a syntax extension for JavaScript that allows you to write
HTML-like code within JavaScript. It gets compiled into JavaScript code.
```jsx
const element = <h1>Hello, world!</h1>;
```
```jsx
const element = <h1>Hello, world!</h1>;
```
Transforms to:
```javascript
const element = React.createElement('h1', null, 'Hello, world!');
```
```jsx
// Class Component
class MyComponent extends React.Component {
render() {
return <h1>Hello, world!</h1>;
}
}
// Functional Component
function MyComponent() {
return <h1>Hello, world!</h1>;
}
```
```bash
npx create-react-app my-app
```
```jsx
function Greeting(props) {
return <h1>Hello, {props.name}!</h1>;
}
// Usage
<Greeting name="Alice" />
```
```jsx
function Parent() {
return <Child message="Hello from Parent" />;
}
function Child(props) {
return <h1>{props.message}</h1>;
}
```
```jsx
class MyComponent extends React.Component {
constructor(props) {
super(props);
this.state = { count: 0 };
}
render() {
return <h1>{this.state.count}</h1>;
}
}
```
```jsx
// Functional Component with useState
function Counter() {
const [count, setCount] = useState(0);
return <button onClick={() => setCount(count + 1)}>{count}</button>;
}
```
- `componentDidMount()`
- `componentDidUpdate()`
- `componentWillUnmount()`
```jsx
class MyComponent extends React.Component {
componentDidMount() {
console.log('Component mounted');
}
componentWillUnmount() {
console.log('Component will unmount');
}
render() {
return <h1>Hello</h1>;
}
}
```
```jsx
function Counter() {
const [count, setCount] = useState(0);
return <button onClick={() => setCount(count + 1)}>{count}</button>;
}
```
```jsx
useEffect(() => {
// Code to run on component mount
console.log('Component mounted');
return () => {
// Cleanup code
console.log('Component unmounted');
};
}, []);
```
```jsx
const MyContext = React.createContext();
```jsx
const ThemeContext = React.createContext('light');
function App() {
return (
<ThemeContext.Provider value="dark">
<Toolbar />
</ThemeContext.Provider>
);
}
function Toolbar() {
return <ThemedButton />;
}
function ThemedButton() {
const theme = useContext(ThemeContext);
return <button>{theme}</button>;
}
```
```jsx
class MyComponent extends React.Component {
constructor(props) {
super(props);
this.myRef = React.createRef();
}
componentDidMount() {
this.myRef.current.focus();
}
render() {
return <input ref={this.myRef} />;
}
}
```
21. **How do you create and use refs?**
- Use `React.createRef()` in class components or `useRef()` in functional
components.
```jsx
// Functional Component
function MyComponent() {
const myRef = useRef(null);
useEffect(() => {
myRef.current.focus();
}, []);
```jsx
import { BrowserRouter as Router, Route, Switch, Link } from 'react-router-dom';
function App() {
return (
<Router>
<nav>
<Link to="/">Home</Link>
<Link to="/about">About</Link>
</nav>
<Switch>
<Route path="/" exact component={Home} />
<Route path="/about" component={About} />
</Switch>
</Router>
);
}
```
```jsx
// Controlled Component
function ControlledForm() {
const [value, setValue] = useState('');
return <input value={value} onChange={e => setValue(e.target.value)} />;
}
// Uncontrolled Component
function UncontrolledForm() {
const inputRef = useRef(null);
return <input ref={inputRef} />;
}
```
```jsx
function Form() {
const [inputValue, setInputValue] = useState('');
return (
<form onSubmit={handleSubmit}>
<input
type="text"
value={inputValue}
onChange={(e) => setInputValue(e.target.value)}
/>
<button type="submit">Submit</button>
</form>
);
}
```
```jsx
function MyButton() {
const handleClick = () => {
alert('Button clicked!');
};
```jsx
function withExtraProps(WrappedComponent) {
return function EnhancedComponent(props) {
return <WrappedComponent extraProp="extra" {...props} />;
};
}
```
```jsx
function List({ items }) {
return (
<ul>
{items.map(item => (
<li key={item.id}>{item.name}</li>
))}
</ul>
);
}
```
```jsx
function List() {
return (
<>
<h1>Title</h1>
<p>Content</p>
</>
);
}
```
```jsx
function List() {
return (
<React.Fragment>
<h1>Title</h1>
<p>Content</p>
</React.Fragment>
);
}
```
33. **What is the difference between React.Fragment and a regular HTML element?
**
- React.Fragment does not create an additional DOM node, while a regular
HTML element does.
```jsx
class ErrorBoundary extends React.Component {
constructor(props) {
super(props);
this.state = { hasError: false };
}
static getDerivedStateFromError() {
return { hasError: true };
}
componentDidCatch(error, info) {
console.log(error, info);
}
render() {
if (this.state.hasError) {
return <h1>Something went wrong.</h1>;
}
return this.props.children;
}
}
```
```jsx
function App() {
return (
<ErrorBoundary>
<MyComponent />
</ErrorBoundary>
);
}
```
```jsx
import PropTypes from 'prop-types';
MyComponent.propTypes = {
name: PropTypes.string.isRequired,
};
```
```jsx
MyComponent.propTypes = {
name: PropTypes.string.isRequired,
age: PropTypes.number,
};
```
```jsx
function MyComponent({ name }) {
return <h1>Hello, {name}</h1>;
}
MyComponent.defaultProps = {
name: 'Guest',
};
```
```jsx
import ReactDOM from 'react-dom';
```jsx
function Modal({ children }) {
return ReactDOM.createPortal(
<div className="modal">{children}</div>,
document.getElementById('modal-root')
);
}
```
```jsx
import { Provider, connect } from 'react-redux';
import { createStore } from 'redux';
function App() {
return (
<Provider store={store}>
<MyComponent />
</Provider>
);
}
function mapStateToProps(state) {
return { data: state.data };
}
```jsx
const MyComponent = React.memo(function ({ value }) {
return <div>{value}</div>;
});
```
```jsx
const MyComponent = React.memo(function ({ value }) {
return <div>{value}</div>;
});
```
```jsx
function MyComponent() {
const [count, setCount] = useState(0);
## Intermediate Questions
```jsx
const LazyComponent = React.lazy(() => import('./LazyComponent'));
function App() {
return (
<React.Suspense fallback={<div>Loading...</div>}>
<LazyComponent />
</React.Suspense>
);
}
```
```jsx
const LazyComponent = React.lazy(() => import('./LazyComponent'));
function App() {
return (
<React.Suspense fallback={<div>Loading...</div>}>
<LazyComponent />
</React.Suspense>
);
}
```
```jsx
function App() {
return (
<React.Suspense fallback={<div>Loading...</div>}>
<LazyComponent />
</React.Suspense>
);
}
```
```jsx
const LazyComponent = React.lazy(() => import('./LazyComponent'));
function App() {
return (
<React.Suspense fallback={<div>Loading...</div>}>
<LazyComponent />
</React.Suspense>
);
}
```
```jsx
useEffect(() => {
// Side effect code here
fetchData();
}, []);
```
```jsx
const initialState = { count: 0 };
function Counter() {
const [state, dispatch] = useReducer(reducer, initialState);
return (
<>
<p>{state.count}</p>
<button onClick={() => dispatch({ type: 'increment' })}>
Increment
</button>
</>
);
}
```
```jsx
function useCustomHook() {
const [value, setValue] = useState(0);
function MyComponent() {
const [value, increment] = useCustomHook();
return (
<div>
<p>{value}</p>
<button onClick={increment}>Increment</button>
</div>
);
}
```
```jsx
function useFetch(url) {
const [data, setData] = useState(null);
const [loading, setLoading] = useState(true);
useEffect(() => {
fetch(url)
.then(response => response.json())
.then(data => {
setData(data);
setLoading(false);
});
}, [url]);
```jsx
function Tabs({ children }) {
const [activeTab, setActiveTab] = useState(0);
return (
<div>
<div>
{React.Children.map(children, (child, index) =>
React.cloneElement(child, {
isActive: index === activeTab,
onClick: () => setActiveTab(index),
})
)}
</div>
<div>
{React.Children.toArray(children)[activeTab].props.children}
</div>
</div>
);
}
```jsx
function Tabs({ children }) {
// Implementation
}
// Usage
function App() {
return (
<Tabs>
<Tab>Tab 1</Tab>
<Tab>Tab 2</Tab>
</Tabs>
);
}
```
```jsx
function AuthProvider({ children }) {
const [isAuthenticated, setIsAuthenticated] = useState(false);
return (
<AuthContext.Provider value={{ isAuthenticated, setIsAuthenticated }}>
{children}
</AuthContext.Provider>
);
}
```
```jsx
function DataProvider({ render }) {
const [data, setData] = useState(null);
useEffect(() => {
// Fetch data
setData(fetchedData);
}, []);
return render(data);
}
function App() {
return (
<DataProvider
render={data => <div>{data ? data : 'Loading...'}</div>}
/>
);
}
```
function App() {
return (
<DataProvider
render={data => <div>{data ? data : 'Loading...'}</div>}
/>
);
}
```
67. **What is the Context API, and how does it replace Redux in certain scenarios?**
- The Context API provides a way to pass data through the component tree
without having to pass props down manually at every level. It can replace Redux for
simpler state management needs.
```jsx
function FileUpload() {
const handleFileChange = (event) => {
const file = event.target.files[0];
// Process file
};
```jsx
const LazyComponent = React.lazy(() => import('./LazyComponent'));
function App() {
return (
<React.Suspense fallback={<div>Loading...</div>}>
<LazyComponent />
</React.Suspense>
);
}
```
```jsx
import { render, screen } from '@testing-library/react';
import MyComponent from './MyComponent';
```jsx
function App() {
return (
<ErrorBoundary>
<MyComponent />
</ErrorBoundary>
);
}
```
```jsx
class ErrorBoundary extends React.Component {
constructor(props) {
super(props);
this.state = { hasError: false };
}
static getDerivedStateFromError() {
return { hasError: true };
}
componentDidCatch(error, errorInfo) {
// Log error
}
render() {
if (this.state.hasError) {
return <h1>Something went wrong.</h1>;
}
return this.props.children;
}
}
```
```jsx
function MyForm() {
const [value, setValue] = useState('');
return (
<form onSubmit={handleSubmit}>
<input type="text" value={value} onChange={handleChange} />
<button type="submit">Submit</button>
</form>
);
}
```
```jsx
class MyPureComponent extends React.PureComponent {
// Implementation
}
```jsx
const inputRef = useRef(null);
function focusInput() {
inputRef.current.focus();
}
```jsx
const FancyInput = React.forwardRef((props, ref) => {
const inputRef = useRef();
useImperativeHandle(ref, () => ({
focus: () => inputRef.current.focus()
}));
```jsx
const MemoizedComponent = React.memo(function Component(props) {
// Implementation
});
```jsx
const memoizedValue = useMemo(() => computeExpensiveValue(a, b), [a, b]);
const memoizedCallback = useCallback(() => { /* callback code */ },
[dependencies]);
```
```jsx
const [state, setState] = useState(initialState);
```jsx
useEffect(() => {
async function fetchData() {
const response = await fetch(url);
const data = await response.json();
setData(data);
}
fetchData();
}, [url]);
```
```jsx
const value = useContext(MyContext);
```
```jsx
const MyContext = React.createContext();
function App() {
return (
<MyContext.Provider value={/* context value */}>
<MyComponent />
</MyContext.Provider>
);
}
function MyComponent() {
const value = useContext(MyContext);
// Use context value
}
```
```jsx
useImperativeHandle(ref, () => ({
focus: () => { /* custom focus method */ }
}));
```
```jsx
import React from 'react';
import ReactDOMServer from 'react-dom/server';
import App from './App';
```bash
npx create-next-app my-next-app
```
```jsx
// pages/[id].js
export default function Page({ id }) {
return <div>Page ID: {id}</div>;
}
98. **What is static site generation (SSG) and how do you implement it in Next.js?**
- SSG generates HTML at build time. Use `getStaticProps` to fetch data and
generate static pages.
```jsx
export async function getStaticProps() {
const data = await fetchData();
return { props: { data } };
}
```
99. **What is server-side rendering (SSR) and how do you implement it in Next.js?**
- SSR generates HTML on each request. Use `getServerSideProps` to fetch data
on each request.
```jsx
export async function getServerSideProps(context) {
const data = await fetchData();
return { props: { data } };
}
```
```jsx
// pages/api/hello.js
export default function handler(req, res) {
res.status(200).json({ message: 'Hello' });
}
```
```jsx
export async function getStaticPaths() {
const paths = await fetchPaths();
return { paths, fallback: false };
}
```
103. **What are API routes in Next.js and how do you use them?**
- API routes allow serverless functions to handle backend logic within the Next.js
application. Define them in the `pages/api` directory.
```jsx
// pages/api/hello.js
export default function handler(req, res) {
res.status(200).json({ message: 'Hello' });
}
```