DEV Community

P Mukila
P Mukila

Posted on

I Learned Today- Basics of React ...

It looks like you're asking about npm, npx, and how they relate to React development. Here's a quick breakdown of each term and how they work together:

Npm (Node Package Manager)

  • npm is the default package manager for Node.js.
  • You use it to install packages (like React) and manage dependencies in your project.

Examples:

npm install react react-dom

Enter fullscreen mode Exit fullscreen mode

Installs React and ReactDOM into your project.

npx (npm execute)

  • npx comes with npm (version 5.2+).
  • It's used to run packages without installing them globally.
  • Very useful for tools like create-react-app. Example:
npx create-react-app my-app
Enter fullscreen mode Exit fullscreen mode

This runs the create-react-app package to set up a new React project called my-app — no need to install it globally.

Typical Workflow for React
1.Create a new React app using npx:

npx create-react-app my-app
cd my-app

Enter fullscreen mode Exit fullscreen mode

2.Start the development server:

npm start
Enter fullscreen mode Exit fullscreen mode

3.Install additional dependencies (if needed):

npm install axios  # for HTTP requests
npm install react-router-dom  # for routing
Enter fullscreen mode Exit fullscreen mode

What is JSX?

JSX is a syntax extension to JavaScript. It lets you write HTML-like code inside JavaScript — commonly used in React.
JSX Example (React)

const element = <h1>Hello, world!</h1>;

Enter fullscreen mode Exit fullscreen mode
  • This looks like HTML, but it’s actually JavaScript with JSX syntax.
  • It gets converted to regular JavaScript before running.

What is JavaScript?

JavaScript is the core programming language used to add behavior to web pages. JSX is built on top of JavaScript.

JS Example

const name = "World";
const message = "Hello, " + name + "!";

Enter fullscreen mode Exit fullscreen mode

How They Work Together in React
JSX + JS Example:

const name = "React";
const element = <h1>Hello, {name}!</h1>;


Enter fullscreen mode Exit fullscreen mode
`JSX: <h1>Hello, {name}!</h1>
JS: const name = "React"; (inside JSX {})
Enter fullscreen mode Exit fullscreen mode

JS and JSX are used together: JS handles logic, JSX handles UI.
`
JSX Compiles to JS

JSX:



const element = <h1>Hello</h1>;
Enter fullscreen mode Exit fullscreen mode

Compiles to:

const element = React.createElement('h1', null, 'Hello');
Enter fullscreen mode Exit fullscreen mode

Framework or Library

React is a JavaScript library, not a framework. It is used to build user interfaces (UIs) by creating reusable components. While React provides a foundation for building UIs, it doesn't dictate how to structure an entire application, making it flexible and adaptable.

Library

A library is a collection of pre-written code components like functions, classes, and modules that streamline development tasks by providing reusable functionality.

  • It only handles the view layer (UI) of your app.
  • It doesn’t include built-in solutions for routing, state management, or form handling.
  • You’re in charge of how to structure your app and what other tools to use.

Framework

While React itself is a library, there are frameworks built on top of React to provide structure and features that React alone doesn’t include.

Why Hooks in React?

React Hooks were introduced to simplify and modernize how we write React components. They solve real problems that existed with class components.

Top Reasons for Using Hooks

1.Simplify Components

Hooks allow you to use state and lifecycle methods without classes.

Before:

class MyComponent extends React.Component {
  state = { count: 0 };
  // lifecycle methods...
}
Enter fullscreen mode Exit fullscreen mode

After:

function MyComponent() {
  const [count, setCount] = useState(0);
}

Enter fullscreen mode Exit fullscreen mode

2.Avoid Repetition

With class components, logic often had to be duplicated or split across lifecycle methods. Hooks let you group related logic together.

3.Better Reusability

Custom hooks let you extract and reuse logic across components — much cleaner than using Higher-Order Components (HOCs) or Render Props.

function useOnlineStatus() {
  const [isOnline, setIsOnline] = useState(true);
  // logic here...
  return isOnline;
}
Enter fullscreen mode Exit fullscreen mode

4.Cleaner Code

Hooks reduce boilerplate and make functional components easier to read and test.

5.Eliminate Confusion with this

In class components, this can be confusing. Hooks don’t use this, which avoids common bugs and improves clarity.

Top comments (0)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.