How to import .tsx file .js file ?

STEP 1: Install TypeScript and Type Definitions

Open your terminal in the project root and run the following command

npm install --save-dev typescript @types/react @types/react-dom

STEP 2: Create or Update tsconfig.json

If you don’t have a tsconfig.json, create one in your project root.
If you have empty one , add the following basic configuration:

{
"compilerOptions": {
"target": "es6",
"module": "esnext",
"jsx": "react-jsx",
"allowJs": true,
"checkJs": false,
"moduleResolution": "node",
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"strict": false,
"resolveJsonModule": true
},
"include": [
"src"
]
}

STEP 3:  Use .tsx Components in .js Files

Now you can import your .tsx files in your .js files as usual as follows

import MyComponent from '../path/to/MyComponent.tsx';

Note:

  • If you use Create React App or Vite, most of this is already set up.
  • You can mix .js.jsx.ts, and .tsx files in your project.
  • If you want TypeScript type checking in .js files, set "checkJs": true in tsconfig.json.

πŸ’‘ Found this helpful? Subscribe for simple React and TypeScript guides with real-world examples. Happy Coding!

What is an Ingress File in Kubernetes? Complete Beginner Guide

When beginners open a project repository, they often see files like:

ingress.yaml

or:

my-app-ingress.yml

and immediately get confused:

πŸ€” What is ingress?
πŸ€” Why do we need it?
πŸ€” What does this file do?
πŸ€” Is it mandatory?
πŸ€” What happens if we don’t use it?

If you are completely new to:

  • Kubernetes
  • DevOps
  • deployments
  • infrastructure

don’t worry.

This guide explains everything in a very simple way with real-world examples.


First Understand the Problem

Imagine you built a React or Node.js application.

Now you deployed it inside Kubernetes.

Question:

πŸ€” How will users access your application from the internet?

Because Kubernetes containers are running internally.

Users outside cannot directly access them.

This is where:

Ingress

comes into the picture.


What is Ingress?

Ingress is:

A way to expose your application to the outside world.

In simple words:

πŸ‘‰ It helps internet users access your application.


Real-World Analogy

Imagine:

  • Your application = a house
  • Kubernetes cluster = a big apartment building
  • Users = visitors

Without ingress:

❌ Visitors don’t know:

  • where to go
  • which room belongs to which application

Ingress acts like:

security guard + receptionist

It:

  • receives requests
  • checks the URL
  • sends users to correct application

What is an Ingress File?

An ingress file is usually:

ingress.yaml

It contains rules that tell Kubernetes:

  • which domain should go where
  • which route should open which service
  • how incoming traffic should be handled

Example Ingress File

apiVersion: networking.k8s.io/v1kind: Ingressmetadata: name: my-app-ingressspec: rules: - host: myapp.com http: paths: - path: / pathType: Prefix backend: service: name: my-app-service port: number: 80

What This File Means

This says:

If user visits myapp.com,send request to my-app-service

What Problem Does Ingress Solve?

Without ingress:

every application may need:

  • separate public IP
  • separate load balancer
  • direct exposure

This becomes:

  • expensive
  • difficult to manage
  • hard to scale

Ingress Solves This

Ingress provides:

  • centralized routing
  • domain-based routing
  • path-based routing
  • SSL handling
  • traffic management

Real Example

Suppose you have:

ApplicationURL
React Appmyapp.com
Admin Panelmyapp.com/admin
API Servermyapp.com/api

Ingress can route all these properly.


Without Ingress

You may need:

3 separate load balancers3 public IPs

With Ingress

One ingress handles everything.


How Request Flows

Request Flow

User opens website ↓Ingress receives request ↓Checks rules ↓Forwards request to correct service ↓Application responds

Important Thing to Understand

Ingress does NOT directly connect to pods.

Flow is:

Ingress β†’ Service β†’ Pods

Beginner-Friendly Architecture

Internet User ↓Ingress ↓Kubernetes Service ↓Pods/Containers

What is a Service?

A service is another Kubernetes component that:

  • connects to pods
  • provides stable networking

Ingress talks to services.

Services talk to pods.


Why We Need Ingress


βœ… 1. Domain Routing

Example:

myapp.comapi.myapp.comadmin.myapp.com

βœ… 2. Path Routing

Example:

/api/admin/profile

βœ… 3. SSL/HTTPS Support

Ingress can handle:

HTTPSTLSSSL certificates

βœ… 4. Centralized Traffic Management

One place to manage all traffic.


βœ… 5. Cost Reduction

Instead of many load balancers:
πŸ‘‰ one ingress can handle multiple apps.


What Happens If We Don’t Use Ingress?

Your app may still work.

BUT:

you may need:

  • NodePort
  • LoadBalancer service
  • direct exposure

Problems Without Ingress

❌ More Public IPs Needed

❌ More Expensive

Cloud load balancers cost money.

❌ Harder Traffic Management

❌ Difficult SSL Handling

❌ Difficult Multi-App Routing

Difference Between Service and Ingress

ServiceIngress
Internal communicationExternal access
Connects to podsRoutes internet traffic
Exposes app inside clusterExposes app outside cluster

What is Ingress Controller?

This is VERY important.

Ingress file alone does nothing.

You also need:

Ingress Controller

What Does It Do?

It actually:

  • reads ingress rules
  • handles traffic
  • applies routing

Popular Ingress Controllers

  • NGINX Ingress Controller
  • Traefik
  • HAProxy
  • AWS ALB Ingress Controller

Without Ingress Controller

Ingress file exists…

BUT:

❌ routing will NOT work.


Real Beginner Confusion

Many beginners think:

Creating ingress.yaml is enough

❌ Wrong.

You also need:

  • ingress controller installed

Example Real Repository Structure

project/ β”œβ”€β”€ src/ β”œβ”€β”€ deployment.yaml β”œβ”€β”€ service.yaml β”œβ”€β”€ ingress.yaml

Why YAML Files?

Kubernetes uses YAML files to define infrastructure.

These files describe:

  • deployments
  • services
  • ingress rules
  • volumes
  • configs

Important Kubernetes Files

FilePurpose
deployment.yamlCreates pods
service.yamlExposes pods internally
ingress.yamlExposes application externally

Simple Full Flow

Full Architecture

User ↓Ingress ↓Service ↓Pod ↓Application

Best Practices

βœ… Use Ingress for Production Apps

βœ… Use HTTPS

Always secure traffic.

βœ… Organize Routes Clearly

βœ… Use One Ingress for Multiple Apps

Saves cost.

Common Beginner Mistakes

❌ Forgetting Ingress Controller

Most common issue.


❌ Wrong Service Name

Ingress routes fail.


❌ Wrong Port Number

Traffic not forwarded properly.


❌ DNS Not Configured

Domain won’t work.


Final Summary

βœ” Ingress exposes applications to internet users
βœ” It routes traffic to correct services
βœ” Helps manage domains and paths
βœ” Reduces infrastructure complexity
βœ” Requires an ingress controller to work
βœ” Commonly defined using ingress.yaml file


πŸ’‘ Found this helpful? Subscribe for beginner-friendly DevOps and Kubernetes guides explained in simple language. Happy Coding!

Fixing TS7016 Error in React: Could Not Find a Declaration File for Module react-router-dom

While working with a React + TypeScript project, you may suddenly see this error:

TS7016: Could not find a declaration file for module 'react-router-dom'

or:

implicitly has an 'any' type

This is a very common error when using TypeScript in React applications.

In this article, we’ll clearly understand:

  • What this error means
  • Why it happens
  • How to fix it
  • What are declaration files
  • Why TypeScript needs them
  • Best practices

The Exact Error

Your error looks like this:

TS7016: Could not find a declaration file for module 'react-router-dom'.'/node_modules/react-router-dom/index.js'implicitly has an 'any' type.

And this line causes the error:

import { NavLink } from 'react-router-dom';

What Does This Error Mean?

TypeScript is saying:

β€œI found the react-router-dom package, but I cannot find its type definitions.”

In simple words:

πŸ‘‰ TypeScript understands JavaScript code
❌ But it does NOT know the types of the package.


Why TypeScript Needs Types

TypeScript works using:

  • types
  • interfaces
  • definitions

Example:

let name: string = "Raju";

TypeScript knows:

  • name must be string

Similarly, TypeScript wants type information for external libraries too.


What Are Declaration Files?

Declaration files are:

.d.ts

files.

These files tell TypeScript:

  • what functions exist
  • what props exist
  • what return types exist

Example

Without types:

function add(a, b) {
return a + b;
}

TypeScript does not know:

  • what a is
  • what b is

With Types

function add(a: number, b: number): number {
return a + b;
}

Now TypeScript understands everything clearly.


Why This Error Happens

Usually because:

  • @types/react-router-dom package is missing
  • TypeScript cannot find declaration files
  • Package version mismatch
  • Incomplete installation

Most Common Fix

Install Type Definitions.

Run:

npm install --save-dev @types/react-router-dom

or

yarn add -D @types/react-router-dom

What This Package Does

This package provides:

type definitions

for:

react-router-dom

Now TypeScript can understand:

  • NavLink
  • Route
  • BrowserRouter
  • useNavigate
  • etc.

After Installation

Restart your React server:

npm start

or

npm run dev

Important Version Issue

This is VERY important.


⚠️ React Router v6+

If you are using:

react-router-dom v6 or later

then many times:

πŸ‘‰ type definitions are already included.

You may NOT need:

@types/react-router-dom

Then Why Error Happens?

Usually because:

  • corrupted node_modules
  • old TypeScript version
  • package mismatch
  • partial installation

Recommended Fix for React Router v6+

Delete:

node_modulespackage-lock.json

Then reinstall:

npm install

Check Installed Version

Run:

npm list react-router-dom

If Using React Router v5

Then install:

npm install --save-dev @types/react-router-dom

because v5 needs separate type definitions.


How TypeScript Reads Packages

When you import:

import { NavLink } from 'react-router-dom';

TypeScript searches for:

1. package2. type definitions3. .d.ts files

If type definitions are missing:

❌ TS7016 error occurs.


Temporary Quick Fix (Not Recommended)

You may see suggestions like:

declare module 'react-router-dom';

inside a .d.ts file.

This removes the error temporarily.

BUT:

❌ You lose type safety.


Why You Should Avoid This

Because TypeScript will treat everything as:

any

which defeats the purpose of TypeScript.


Best Solution

Always use proper type definitions.


❌ Before Fix

import { NavLink } from 'react-router-dom';

Error:

Could not find declaration file

βœ… After Fix

npm install --save-dev @types/react-router-dom

Now error disappears.


Another Possible Issue

Sometimes developers accidentally install:

react-router

instead of:

react-router-dom

Make sure correct package exists.


Verify in package.json

"react-router-dom": "^6.x.x"

Recommended TypeScript Setup

Install:

npm install typescript @types/react @types/react-dom

Important Learning

JavaScript libraries work WITHOUT types.

But TypeScript applications need:

  • declarations
  • interfaces
  • type definitions

to provide:

  • autocomplete
  • validation
  • error checking
  • IntelliSense

Final Summary

βœ” TS7016 means TypeScript cannot find type definitions
βœ” Usually happens with missing @types/... packages
βœ” Install correct type definitions
βœ” Restart development server after installation
βœ” React Router v6+ usually includes types already
βœ” Version mismatch can also cause this error


Quick Fix Checklist

Before debugging deeply, check:

βœ… react-router-dom installed
βœ… correct version installed
βœ… @types/react-router-dom installed (for v5)
βœ… restart server
βœ… delete node_modules and reinstall if needed


πŸš€ Call to Action

πŸ’‘ Found this helpful? Subscribe for simple React and TypeScript debugging guides with real-world examples. Happy Coding!

Can We Use Both .js and .tsx Files in a React Project? (Complete Guide)

Many developers moving from JavaScript to TypeScript have this question:

πŸ€” β€œCan I use both .js and .tsx files in the same React project?”

The answer is:

βœ… YES β€” absolutely.

In fact, many real-world React applications use a mix of:

  • .js
  • .jsx
  • .ts
  • .tsx

especially during migration from JavaScript to TypeScript.

In this guide, you’ll learn:

  • What .js and .tsx files are
  • Can they work together
  • Real-world usage
  • Benefits and drawbacks
  • Best practices

πŸ“Œ Understanding File Types

Before understanding mixed usage, let’s quickly understand each file type.


πŸ”Ή What is .js?

.js means:

JavaScript file

Example:

function add(a, b) {
return a + b;
}

πŸ”Ή What is .tsx?

.tsx means:

TypeScript + JSX file

Used when:

  • Writing React components
  • Using TypeScript types

Example:

type Props = {
name: string;
};
function User({ name }: Props) {
return <h1>{name}</h1>;
}

Can They Work Together?

YES βœ…

You can absolutely use:

  • .js
  • .jsx
  • .ts
  • .tsx

inside the same React project.


Real-World Scenario

Many companies:

  • Start project in JavaScript
  • Slowly migrate to TypeScript

So they temporarily have:

src/ β”œβ”€β”€ App.tsx β”œβ”€β”€ Header.jsx β”œβ”€β”€ utils.js β”œβ”€β”€ api.ts └── Dashboard.tsx

πŸ‘‰ This is completely normal.


How Does It Work?

When using TypeScript in React:

πŸ‘‰ TypeScript compiler can understand JavaScript files too.


Important Configuration

In tsconfig.json:

{
"compilerOptions": { "allowJs": true }
}

What Does allowJs Do?

"allowJs": true

πŸ‘‰ Allows TypeScript to compile .js files also.

Without this:

❌ TypeScript may ignore JS files.


Example Project Structure

Example

src/ β”œβ”€β”€ components/ β”‚ β”œβ”€β”€ Button.tsx β”‚ β”œβ”€β”€ Navbar.jsx β”‚ β”œβ”€β”€ utils/ β”‚ β”œβ”€β”€ math.js β”‚ β”œβ”€β”€ api.ts β”‚ └── App.tsx

πŸ‘‰ All these files can work together.


Importing Between JS and TSX


βœ… Import JS into TSX

import add from "./utils/math";

βœ… Import TSX Component

import Button from "./components/Button";

Why Developers Mix .js and .tsx


βœ… 1. Gradual Migration

Big applications cannot migrate instantly.

So teams:

  • Convert slowly
  • File by file

βœ… 2. Legacy Code Support

Old JavaScript files may still work perfectly.


βœ… 3. Faster Development

Some utility files may remain simple .js.


Important Differences

Feature.js.tsx
Type Safety❌ Noβœ… Yes
IntelliSenseLimitedBetter
Compile ChecksβŒβœ…
React JSX Supportβœ…βœ…

Benefits of Using .tsx

βœ… Better Error Detection

TypeScript catches mistakes early.


βœ… Strong Type Safety

type User = { name: string; };

βœ… Better Developer Experience

  • Auto suggestions
  • Safer refactoring
  • Better maintainability

Problems You May Face

❌ 1. Type Errors with JS Files

Sometimes TypeScript cannot understand JS structure properly.


❌ 2. Inconsistent Codebase

Mixing too many styles may confuse developers.


❌ 3. Any-Type Problems

JS files may reduce type safety.


Best Practice (Recommended)

βœ… Recommended Approach

If possible:

πŸ‘‰ Gradually move toward TypeScript


πŸš€ Good Strategy

Step 1

Keep old files in JS.


Step 2

Write new components in TSX.


Step 3

Slowly migrate old files.


Example Migration

Old JS Component

function Button(props) {
return <button>{props.title}</button>;
}

Migrated TSX Component

type Props = { title: string;};
function Button({ title }: Props) {
return <button>{title}</button>;
}

Important Note About .ts vs .tsx

ExtensionPurpose
.tsTypeScript without JSX
.tsxTypeScript with JSX

Interview Question

❓ Can we use .js and .tsx together in React?

πŸ‘‰ Answer:

Yes. React projects can use both JavaScript and TypeScript files together. TypeScript supports gradual migration using allowJs.


Final Summary

βœ” .js and .tsx can work together
βœ” Common in real-world migration projects
βœ” Use allowJs: true in TypeScript config
βœ” TSX provides better type safety
βœ” Gradual migration is best practice


πŸ’‘ Found this helpful? Subscribe for simple React and TypeScript guides with real-world examples. Happy Coding!

React.memo and Composition in React (Complete Guide with Detailed Examples)

When building React applications, two concepts help developers create:

βœ… Faster applications
βœ… Cleaner code
βœ… Reusable components
βœ… Better maintainability

Those concepts are:

  • React.memo
  • Component Composition

In this guide, you’ll learn both concepts clearly with real-world examples.


What You Will Learn

βœ” What is React.memo
βœ” Why we need it
βœ” How it improves performance
βœ” What is Composition
βœ” Why Composition is preferred in React
βœ” Real-world examples for both


Part 1: Understanding React.memo

What is React.memo?

Simple Definition

React.memo is a higher-order component that prevents unnecessary re-rendering of functional components.


Why Do We Need React.memo?

In React:

πŸ‘‰ Parent component re-renders
➑ Child components also re-render

Even if:

  • Props did not change
  • UI did not change

This can reduce performance.


Example Without React.memo

❌ Code

import { useState } from "react";

function Child() {
console.log("Child Rendered");
return <h2>Child Component</h2>;
}

function App() {
const [count, setCount] = useState(0);
return (
<div>
<h1>{count}</h1>
<button onClick={() => setCount(count + 1)}>
Increment
</button>
<Child />
</div>
);
}

export default App;

What Happens Here?

Whenever count changes:

setCount(count + 1)

πŸ‘‰ Parent re-renders
πŸ‘‰ Child ALSO re-renders

Even though Child has no relation to count.


Optimized with React.memo

βœ… Code

import { memo } from "react";

const Child = memo(function Child() {
console.log("Child Rendered");
return <h2>Child Component</h2>;
});

export default Child;

Now What Happens?

When parent re-renders:

πŸ‘‰ React compares previous props vs new props

If props are same:

βœ… Child render is skipped


Important Rule

React.memo works best when:

βœ” Props are stable
βœ” Component renders frequently
βœ” Rendering is expensive


⚠️ Example Where React.memo Fails

❌ Problem

<Child data={{ name: "React" }} />

Why?

Because:

{ name: "React" }

creates a NEW object every render.

πŸ‘‰ React thinks props changed.


Fix with useMemo

const data = useMemo(() => ({
name: "React"
}), []);

Real-World Example

Imagine:

  • Dashboard with charts
  • Huge tables
  • Complex UI

Without optimization:

❌ Unnecessary renders
❌ Slow UI

With React.memo:

βœ… Better performance


When NOT to Use React.memo

Avoid if:

  • Component is very small
  • Props change frequently
  • Optimization not needed

πŸ‘‰ Overusing memo can increase complexity.


Interview Tip

❓ What is React.memo?

πŸ‘‰ Answer:

React.memo prevents unnecessary re-rendering of functional components by memoizing rendered output based on props comparison.


Part 2: Understanding Composition in React

πŸ“Œ What is Composition?

Simple Definition

Composition means building complex UI using smaller reusable components.


Basic Composition Example

πŸ”Ή Button Component

function Button({ children }) {
return (
<button>
{children}
</button>
);
}

πŸ”Ή Usage

<Button>Save</Button>
<Button>Delete</Button>

Why Composition is Powerful

Composition helps:

βœ… Reusability
βœ… Cleaner architecture
βœ… Scalability
βœ… Separation of concerns


Composition with Layouts

Card Component

function Card({ children }) {
return (
<div className="card">
{children}
</div>
);
}

Usage

<Card>
<h2>React</h2>
<p>Composition Example</p>
</Card>

Composition Using Props

Modal Component

function Modal({ title, content, footer }) {
return (
<div className="modal">
<h2>{title}</h2>
<div>{content}</div>
<footer>{footer}</footer>
</div>
);
}

Usage

<Modal
title="Delete User"
content={<p>Are you sure?</p>}
footer={<button>Confirm</button>}
/>

Composition vs Inheritance

React documentation recommends:

βœ… Composition
❌ Inheritance


Inheritance Problem

Inheritance creates:

  • Tight coupling
  • Complexity

Composition Advantage

Composition provides:

  • Flexibility
  • Better maintainability

Advanced Composition Pattern

Compound Components

Example:

<Tabs>
<Tabs.List />
<Tabs.Panel />
</Tabs>

Used in:

  • UI libraries
  • Design systems

Combining React.memo + Composition

This is common in real applications.

Example

const Card = memo(function Card({ children }) {
return (
<div className="card">
{children}
</div>
);
});

πŸ‘‰ Reusable + optimized


Common Mistakes

❌ Overusing React.memo

Not every component needs memoization.


❌ Creating Huge Components

Break UI into reusable pieces.


❌ Deep Prop Drilling

Use:

  • Context API
  • Composition patterns

Best Practices

βœ… Use React.memo Carefully

Optimize only where needed.


βœ… Prefer Composition

Build UI using reusable blocks.


βœ… Keep Components Small

Small components:

  • Easier to test
  • Easier to reuse

Final Summary

React.memo

βœ” Prevents unnecessary re-renders
βœ” Improves performance
βœ” Best for stable props


Composition

βœ” Builds reusable UI
βœ” Cleaner architecture
βœ” Recommended React pattern


πŸ’‘ Found this helpful? Subscribe for simple React guides, real-world examples, and interview preparation tips. Happy Coding!