Category: Notes

  • How to create a React App and add Bootstrap to it

    Creating a React app with Bootstrap installed by default is straightforward! Here’s a step-by-step guide to do it in the Ubuntu terminal:


    Step 1: Install Node.js and npm

    React requires Node.js and npm (Node Package Manager). If you don’t already have them installed, run:

    sudo apt update
    sudo apt install nodejs npm

    Verify the installation:

    node -v
    npm -v

    Step 2: Create a React App

    Use create-react-app to set up a new React project. Run:

    npx create-react-app my-react-app

    Replace my-react-app with your desired project name.


    Step 3: Navigate to the Project Directory

    Move into the project folder:

    cd my-react-app

    Step 4: Install Bootstrap

    Install Bootstrap using npm:

    npm install bootstrap

    Step 5: Import Bootstrap in Your React App

    Open the src/index.js or src/App.js file and add the following line at the top to import Bootstrap:

    import 'bootstrap/dist/css/bootstrap.min.css';

    This imports the Bootstrap CSS file into your project.


    Step 6: Run the React App

    Start the development server to see your app in action:

    npm start

    Your app will open in the browser at http://localhost:3000.


    Step 7: Verify Bootstrap is Working

    To test if Bootstrap is working, replace the content of src/App.js with this simple Bootstrap-styled component:

    import React from 'react';
    import 'bootstrap/dist/css/bootstrap.min.css';
    
    function App() {
      return (
        <div className="container mt-5">
          <h1 className="text-primary">Hello, Bootstrap in React!</h1>
          <button className="btn btn-success">Click Me</button>
        </div>
      );
    }
    
    export default App;

    Save the file, and you should see a Bootstrap-styled heading and button in your browser.


    Optional: Install React-Bootstrap (Advanced)

    If you want to use React-Bootstrap (a library that provides Bootstrap components as React components), you can install it:

    npm install react-bootstrap

    Then, import components like this:

    import Button from 'react-bootstrap/Button';

    Summary

    1. Install Node.js and npm.
    2. Create a React app using create-react-app.
    3. Install Bootstrap with npm install bootstrap.
    4. Import Bootstrap’s CSS in index.js or App.js.
    5. Run the app with npm start.

    That’s it! You now have a React app with Bootstrap installed and ready to use. Let me know if you need further assistance! 🚀

  • 2025-02-10

    You know one of the things that I really should consider is that learning programming languages is just about staying around. Cuz it gets really boring or hard at times.

    I think learning to code is all about facing different kinds of problems and errors and trying to either bypass them or solve them.

    https://create-react-app.dev/docs/folder-structure

    What is the difference between props and states:

    In React, both props (properties) and state are essential for managing data and passing information between components. Props are used to pass data from a parent component to its children, ensuring a one-way data flow.234+3 They are immutable and cannot be modified within the child component.234+3 On the other hand, state allows components to manage and update their internal data, which can be modified within the component but not accessed from outside.234+3 State is mutable and can be updated using the setState method.234+3 Changes in props or state lead a component to re-render, and understanding when and how this happens is crucial for optimized React applications.456+1

    • Props: Used for passing data between components, typically from parent to child. They are immutable and cannot be changed within the component.234+3
    • State: Used for managing data within components. It allows components to change their output over time in response to user actions, network responses, or other events.234+3

    Understanding the differences between props and state is essential for building robust and scalable React applications.

    How to create a react app?

    npx create-react-app my-app

    How to run your app and see it live?

    npm start

    What is DOM?

    a programming interface for web documents that represents the page so that programs can change the document structure, style, and content.

    What are Classes in JS?

    A class is a type of function, but instead of using the keyword function to initiate it, we use the keyword class, and the properties are assigned inside a constructor() method.
    https://www.w3schools.com/react/react_es6_classes.asp

    You can add your own methods in a class:

    How to create class inheritance?

    To create a class inheritance, use the extends keyword.

    A class created with a class inheritance inherits all the methods from another class:

    What is this?

    In regular functions the this keyword represented the object that called the function, which could be the window, the document, a button or whatever.

    How does React Display?

    The createRoot() function takes one argument, an HTML element.

    The purpose of the function is to define the HTML element where a React component should be displayed.

    The render() method is then called to define the React component that should be rendered.

    Display a paragraph inside an element with the id of “root”:

    const container = document.getElementById('root');
    const root = ReactDOM.createRoot(container);
    root.render(<p>Hello</p>);

    This will result in this:

    <body>
      <div id="root"></div>
    </body>

    Attribute class = className

    The class attribute is a much used attribute in HTML, but since JSX is rendered as JavaScript, and the class keyword is a reserved word in JavaScript, you are not allowed to use it in JSX.

    Use attribute className instead.

    How to use if in REACT

    const x = 5;
    
    const myElement = <h1>{(x) < 10 ? "Hello" : "Goodbye"}</h1>;
    

    Class Component

    A class component must include the extends React.Component statement. This statement creates an inheritance to React.Component, and gives your component access to React.Component’s functions.

    The component also requires a render() method, this method returns HTML.

    ExampleGet your own React.js Server

    Create a Class component called Car

    class Car extends React.Component {
      render() {
        return <h2>Hi, I am a Car!</h2>;
      }
    }

    Components can be passed as props, which stands for properties.

    Props are like function arguments, and you send them into the component as attributes.

    Props are arguments passed into React components.

    Props are passed to components via HTML attributes.

    There are two methods that we gonna use a lot:

    useEffect and useState

    useEffect runs with each refresh

  • 2025-02-07

    Here are A few algebraic equations I have taken from ChatGPT to solve using SymPy in Google Colab. Each equation is structured in a way that you can easily plug it into your existing code. ChatGPT has provided a variety of linear equations, quadratic equations, and some simple polynomial equations to give you a broad range of problems.

    You can access them both on Google Colab or Github:

    https://github.com/mateenkhadem/College-Algebra/blob/main/2025_02_07.ipynb

    https://colab.research.google.com/drive/19jKNZlaX-Xs85Qe__95nQVAW19YJVVT4?usp=sharing

  • A Note on Conditional Operators

    There are a few things that I have learned today and I would like to mention them here so I can come back to them in the future.

    The Difference Between == And ===

    First of all, the difference between these two in JavaScript is that the first one compares whether the value of two things are the same but the second one also compares the datatype. So it’s like the second one is Value & Data Type (in a logical way).

    Comparison operators return a boolean value.

    Strings are Immutable!

    Meaning that when you use a string method, the method doesn’t change the string but rather it actually creates a new string.

    The boolean NOT operator is represented with an exclamation sign !

    Another Way to write conditions

    let accessAllowed = (age > 18) ? true : false;