Skip to content

Commit 0199e91

Browse files
authored
Update README.md
1 parent 0580cb8 commit 0199e91

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

README.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,3 +139,59 @@ MongoDB is not vulnerable to SQL injection as it doesn't use SQL queries. Howeve
139139
- Validate inputs: Use libraries like Joi.
140140
- Use parameterized queries: Although MongoDB queries are not SQL-based, always treat user input cautiously.
141141
- Sanitize inputs: Remove any potentially harmful characters or strings.
142+
### 16. What are React hooks, and how do you create a custom hook?
143+
React hooks are functions that let you use state and other React features in functional components. A custom hook is a function that starts with use and can call other hooks to encapsulate reusable logic.
144+
```
145+
function useFetch(url) {
146+
const [data, setData] = useState(null);
147+
useEffect(() => {
148+
fetch(url)
149+
.then((response) => response.json())
150+
.then((data) => setData(data));
151+
}, [url]);
152+
return data;
153+
}
154+
```
155+
### 17. How do you handle file uploads in a Node.js application?
156+
File uploads in a Node.js application can be handled using the multer middleware.
157+
```
158+
const multer = require('multer');
159+
const upload = multer({ dest: 'uploads/' });
160+
161+
app.post('/upload', upload.single('file'), (req, res) => {
162+
res.send('File uploaded successfully');
163+
});
164+
```
165+
### 18. Explain the virtual DOM and how React uses it to optimize rendering.
166+
The virtual DOM is a lightweight representation of the actual DOM. React uses it to optimize rendering by comparing the virtual DOM with the actual DOM and updating only the parts that have changed (reconciliation). This minimizes direct manipulation of the actual DOM, leading to better performance.
167+
### 19. How does the MERN stack handle real-time data?
168+
Real-time data in a MERN stack application can be handled using WebSockets or libraries like Socket.io. The server (Node.js + Express) sets up a WebSocket connection to communicate with the client (React) in real-time.
169+
```
170+
const io = require('socket.io')(server);
171+
io.on('connection', (socket) => {
172+
socket.on('message', (data) => {
173+
io.emit('message', data);
174+
});
175+
});
176+
```
177+
### 20. How would you implement server-side rendering (SSR) in a MERN stack application?
178+
Server-side rendering (SSR) in a MERN stack application can be implemented using libraries like Next.js for the React frontend. Next.js handles SSR out of the box and can be integrated with a Node.js/Express backend.
179+
```
180+
const express = require('express');
181+
const next = require('next');
182+
const app = next({ dev: process.env.NODE_ENV !== 'production' });
183+
const handle = app.getRequestHandler();
184+
185+
app.prepare().then(() => {
186+
const server = express();
187+
188+
server.get('*', (req, res) => {
189+
return handle(req, res);
190+
});
191+
192+
server.listen(3000, (err) => {
193+
if (err) throw err;
194+
console.log('> Ready on http://localhost:3000');
195+
});
196+
});
197+
```

0 commit comments

Comments
 (0)