React Components
React Components
• React components are the building blocks of a React application. They are reusable
pieces of code that define how a portion of the user interface should appear and
behave.
• There are two main types of components in React:
• 1. Functional Components: Functional components are simple JavaScript functions
that return JSX. They are often used for presentational purposes.
• Example: function Greeting(props) {
return <h1>Hello, {props.name}!</h1>;}
• 2. Class Components: Class components are more complex and are defined using
ES6 classes. They can hold their own state and lifecycle methods.
• Example: class Greeting extends React.Component {
render() {
return <h1>Hello, {this.props.name}!</h1>;
}
}
KEY FEATUERS:
• Props: Components can receive input via props (properties), allowing them to be
reusable with different data.
• State: Class components can manage their own state, allowing them to respond to
user input and changes in data.
• Lifecycle Methods: Class components have lifecycle methods (like
componentDidMount, componentDidUpdate, and componentWillUnmount) that let
you run code at specific points in a component’s life.
• Hooks: Functional components can use hooks (like useState, useEffect) to manage
state and side effects.
• Characteristics of React Components
• 1. Reusability: React components are designed to be reusable, making it easy to
build complex UIs from smaller, reusable pieces.
• 2. Encapsulation: React components encapsulate their own state and behavior,
making it easy to manage complexity.
• 3. Composition: React components can be composed together to form more
complex UIs.
ISSUE TRACKER
• These applications help you create a bunch of issues or bugs, assign them to people,
and track their statuses. The Issue Tracker application aims to provide a simple and
efficient way to manage issues or bugs
• These are essentially CRUD applications (Create, Read, Update, and Delete a record
in a database) that manage a list of objects or entities.
• The CRUD pattern is so useful because pretty much all enterprise applications are
built around the CRUD pattern on different entities or objects.
• In the case of the Issue Tracker, we’ll only deal with a single object or record,
because that’s good enough to depict the pattern.
• Once you grasp the fundamentals of how to implement the CRUD pattern in MERN,
you’ll be able to replicate the pattern and create a real-life application.
Here’s the requirement list for the Issue Tracker application, a simplified or toned-down
version of GitHub Issues or Jira:
• The Issue Tracker application should provide the following features:
• 1. View Issue List: Display a list of all issues, with filtering options by various
parameters.
• 2. Add New Issue: Allow users to create new issues by providing initial values for the
issue's fields.
• 3. Edit and Update Issue: Enable users to edit and update existing issues by changing
their field values.
• 4. Delete Issue: Allow users to delete existing issues.
An issue should have following attributes:
• A title that summarizes the issue (freeform long text)
• An owner to whom the issue is assigned (freeform short text)
• A status indicator (a list of possible status values)
• Creation date (a date, automatically assigned)
• Effort required to address the issue (number of days, a number)
• Estimated completion date or due date (a date, optional)
REACT CLASSES
EVENT HANDLING
• ...
• <div>This is a placeholder for a form to add an issue.</div>
• <form>
• <input type="text" name="owner" placeholder="Owner" />
• <input type="text" name="title" placeholder="Title" />
• <button>Add</button>
• </form>
• ...
• At this point, we can remove the timer that creates an issue from the constructor.
• ...
• constructor() {
• super();
• setTimeout(() => {
• this.props.createIssue(sampleIssue);
• }, 2000);
• }
• ...
ROUTING
• Routing in Express.js refers to how an application responds to client requests (such
as GET, POST, PUT, DELETE) to specific endpoints (URLs).
• With Express, defining routes is very easy and flexible. Routes map HTTP requests to
specific functions, which is crucial for building APIs and web pages.
KEY CONCEPTS
• Route Definition: A route defines the URL and HTTP method (e.g., GET, POST) that
an application will listen for and respond to.
• Route Handlers: The functions that execute when a route is matched. These
functions can send back responses, interact with databases, perform business logic,
etc.
• Route Parameters: Dynamic placeholders in a URL that can capture values, like user
IDs or product names.
• Query Parameters: Parameters appended to the URL, typically after a ?, that are
used to pass data in a URL.
BASIC SYNTAX:
• To define a route in Express, we use app.METHOD() where METHOD is the HTTP
method (such as get, post, put, or delete), and the first argument is the path.
• app.get('/home', (req, res) => {
• res.send('Welcome to the Home Page!');
• });
• app.get(): This defines a route that listens for GET requests.
• /home: The URL or path the route is associated with.
MIDDLEWARE
• Middleware functions are an essential concept that enables the processing of
requests and responses in the application pipeline.
• Middleware functions are functions that have access to the request object (req), the
response object (res), and the next middleware function in the application's request-
response cycle.
• Middleware functions are executed sequentially in the order they are defined.
• Middleware can end the request-response cycle by sending a response or call next()
to pass control to the next middleware.
Application-Level Middleware
• These are bound to an instance of the Express application using app.use() or specific
HTTP methods like app.get() or app.post().
Router-Level Middleware
• These work similarly to application-level middleware but are specific to an instance
of express.Router().
Error-Handling Middleware
• Error-handling middleware must have four arguments: (err, req, res, next).
ERROR-HANDLING MIDDLEWARE
app.use((err, req, res, next) => {
• console.error(err.stack);
• res.status(500).send('Something broke!');
• });
REST API
• REST API (Representational State Transfer Application Programming Interface) is a
set of rules and conventions for building and interacting with web services.
• REST is an architectural style that allows systems to communicate using stateless,
client-server protocols, primarily HTTP.
• REST APIs are widely used for creating scalable, maintainable, and flexible web
services.
Although the HTTP method and operation mapping are well mapped and specified, REST by
itself lays down no rules for the following:
• Filtering, sorting, and paginating a list of objects. The query string is commonly used
in an implementation-specific way to specify these.
• Specifying which fields to return in a READ operation.
• If there are embedded objects, specifying which of those to expand in a READ
operation.
• Specifying which fields to modify in a PATCH operation.
• Representation of objects. You are free to use JSON, XML, or any other
representation for the objects in both READ and WRITE operations.
GRAPHQL
What is GraphQL?