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!