Friday, August 18, 2023

React : window.scrollTo or move page to a position

 

Certainly, if you want to use `window.scrollTo` or equivalent methods to scroll the page, here are some examples:

 

1. **Using `window.scrollTo` with Coordinates**:

 

```tsx

const scrollToPosition = (yPosition: number) => {

    window.scrollTo({ top: yPosition, behavior: 'smooth' });

};

// Example usage

scrollToPosition(500); // Scroll to y position 500

```

In this example, `window.scrollTo` is used to scroll to a specific y-position on the page. The `behavior: 'smooth'` option provides smooth scrolling.

2. **Using Element's `scrollIntoView` Method**:

```tsx

const scrollToElement = (elementId: string) => {

    const element = document.getElementById(elementId);

    if (element) {

        element.scrollIntoView({ behavior: 'smooth' });

    }

};

// Example usage

scrollToElement('myTargetElement'); // Scroll to element with id "myTargetElement"

```

In this example, the `scrollIntoView` method is used on an HTML element with a specified ID. The element will be scrolled into view with smooth scrolling.

 

3. **Using `scrollTo` or `scrollBy` on Specific Element**:

 

```tsx

const scrollToElement = (elementId: string, yOffset: number = 0) => {

    const element = document.getElementById(elementId);

    if (element) {

        const yPosition = element.getBoundingClientRect().top + window.pageYOffset + yOffset;

        window.scrollTo({ top: yPosition, behavior: 'smooth' });

    }

};

// Example usage

scrollToElement('myTargetElement', -100); // Scroll to element with id "myTargetElement" with a yOffset of -100 pixels

```

In this example, `getBoundingClientRect` is used to calculate the y-position of an element relative to the viewport. You can adjust the yOffset to fine-tune the scrolling position.

Remember that smooth scrolling behavior might not work in all browsers or under all circumstances. It's generally supported in modern browsers. If smooth scrolling doesn't work as expected, you can use the default behavior by omitting the `behavior` option or replacing `'smooth'` with `'auto'`.

yup vs formik in React TS

 Both "yup" and "Formik" are popular libraries in the React ecosystem, especially when it comes to handling forms and form validation. Let's take a look at what each library does and how they compare in the context of React with TypeScript.


1. **yup:**

   "yup" is a schema validation library that focuses on defining and validating data schemas, often used for form input validation. It provides a simple and declarative way to define validation rules for your data structures. "yup" is not specifically designed for form management but rather for validating data before it gets submitted to APIs or stored in a database.


   **Pros of yup:**

   - Declarative schema validation.

   - Works well with form validation scenarios.

   - Provides powerful validation and transformation capabilities.

   - Schema can be reused across different parts of your application.


   **Cons of yup:**

   - It's primarily focused on validation and doesn't handle form state management.

   - Doesn't offer built-in form handling features like handling form submissions, tracking form values, etc.


2. **Formik:**

   "Formik" is a library that provides a set of tools and utilities for handling forms in React. It helps manage form state, form submission, and validation. While Formik itself doesn't provide schema validation, it can work seamlessly with validation libraries like "yup" to achieve comprehensive form handling.


   **Pros of Formik:**

   - Offers a complete solution for form management, including state, submission, and validation.

   - Integrates well with various validation libraries, including "yup."

   - Provides a way to manage form fields and their values efficiently.

   - Supports handling complex form validation scenarios.


   **Cons of Formik:**

   - Might have a bit of a learning curve for complex use cases.

   - Formik's API and concepts might seem overwhelming for simpler forms.


**Using them together:**

A common approach is to use "Formik" for managing form state and submission while using "yup" for schema validation. This combination leverages the strengths of both libraries. Formik provides an excellent framework for handling form state and user interactions, while "yup" takes care of data validation based on the defined schemas.


In a TypeScript React application, you can benefit from TypeScript's type checking to ensure that your form state and validation schemas align correctly.


In summary, if you're looking for a comprehensive solution for handling forms, especially in a TypeScript environment, using "Formik" for form management and combining it with "yup" for schema validation is a strong approach.

Sunday, July 23, 2023

Deploying Jakarta EE on Payara Server & Maven vs. Embedded Server and Maven: Pros and Cons

Deploying Jakarta EE on Payara Server & Maven vs. Embedded Server and Maven: Pros and Cons


Introduction:

Deploying Jakarta EE applications is essential for Java developers, and there are multiple approaches to achieve it. Two popular methods are using Payara Server and Maven for traditional deployment, and employing an embedded server with Maven for a lightweight setup. Each approach has its advantages and disadvantages, and in this post, we'll explore the key differences between these two deployment strategies.


1. Traditional Deployment with Payara Server & Maven:

- Payara Server: Payara Server is a robust application server built on GlassFish Server Open Source Edition. It provides full support for Jakarta EE and MicroProfile standards and comes with enterprise-level features like clustering, high availability, and monitoring.

- Maven: Maven is a powerful build tool that manages project dependencies, compiles source code, and packages applications into distributable artifacts. It simplifies the build process and helps automate various tasks during development.


Pros:

a. Production-Ready: Payara Server is designed for enterprise-level applications, making it a solid choice for deploying mission-critical applications.

b. Full Jakarta EE Support: With Payara Server, developers can take advantage of the entire Jakarta EE ecosystem and utilize a wide range of enterprise technologies.

c. Scalability: Payara Server's clustering capabilities enable horizontal scaling, ensuring high performance and availability.


Cons:

a. External Server Dependency: Users need to install Payara Server separately, which might introduce additional prerequisites and complexities for running the application.

b. Heavier Footprint: Deploying on a standalone server can result in a larger application footprint, which may not be ideal for lightweight or simple projects.


2. Embedded Server and Maven:

- Embedded Server: An embedded server, like Payara Micro or Tomcat, allows developers to package the server within the application, making it self-contained and runnable without external server dependencies.

- Maven: As mentioned earlier, Maven is a build automation tool that streamlines the application's build process.


Pros:

a. Simplified Development: Using an embedded server and Maven eliminates the need for a separate server installation, making it easier for developers to focus solely on the application code.

b. Lighter Footprint: The embedded approach results in a smaller application size, making it suitable for quick demos or microservices.

c. Easy Distribution: The self-contained nature of the application simplifies distribution and deployment.


Cons:

a. Limited Features: Embedded servers may not support the full range of features provided by standalone servers like Payara Server.

b. Production Considerations: While ideal for development and testing, the embedded approach might not be the best fit for production environments that require advanced server features.


Conclusion:

Choosing between deploying Jakarta EE on Payara Server & Maven versus using an embedded server and Maven largely depends on the project's requirements and objectives. For enterprise-grade applications requiring full Jakarta EE support, the traditional deployment approach with Payara Server is a reliable choice. On the other hand, for lightweight projects or development and testing purposes, opting for an embedded server and Maven provides simplicity and ease of use.


Ultimately, developers should carefully evaluate their specific needs, scalability concerns, and production requirements before making the final decision. Both approaches have their merits, and selecting the right one will ensure a successful and efficient Jakarta EE deployment process.

Sunday, May 21, 2023

Ultimate Guide: a Jakarta EE HelloWorld App with Payara Server & Maven! 🔥


Create a new Maven project:
mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-webapp -DgroupId=com.example -DartifactId=hello-maven-project

Navigate to the project directory:
cd hello-maven-project

Open the pom.xml file in a text editor and add the following dependencies inside the <dependencies> section:
<dependency>
    <groupId>jakarta.platform</groupId>
    <artifactId>jakarta.jakartaee-api</artifactId>
    <version>8.0.0</version>
    <scope>provided</scope>
</dependency>