1.1.2 • Published 10 months ago

use-flexible-effect v1.1.2

Weekly downloads
-
License
MIT
Repository
github
Last release
10 months ago

useFlexibleEffect

useFlexibleEffect is a React custom hook that behaves like useEffect but can be conditionally triggered within a function, allowing for more flexible execution control.

Usage

Here’s an example of how to use the useFlexibleEffect hook:

import { useEffect, useState } from "react";
import UseFlexibleEffect from "use-flexible-effect";

const App = () => {
  const [isLoggedIn, setIsLoggedIn] = useState(false);
  const [userData, setUserData] = useState(null);

  // Initialize useConditional to get the conditional effect function
  const triggerEffect = UseFlexibleEffect();

  const handleSignIn = async () => {
    console.log("Signing in...");
    await new Promise((resolve) => setTimeout(resolve, 1000));

    const success = true;
    if (success) {
      setUserData({ name: "John Doe", email: "john@example.com" });
      setIsLoggedIn(true);
    } else {
      console.error("Sign-in failed");
    }
  };

  if(isLoggedIn){
    triggerEffect(() => {
      console.log("User data loaded:", userData);
    }, [userData]);
  }

  return (
    <div style={{ textAlign: "center", marginTop: "50px" }}>
      <h1>Testing Conditional useFlexibleEffect</h1>
      {isLoggedIn ? <p>Welcome, {userData?.name}!</p> : <button onClick={handleSignIn}>Sign In</button>}
    </div>
  );
};

export default App;

Installation

npm install use-flexible-effect
1.1.2

10 months ago

1.1.1

10 months ago

1.1.0

10 months ago

1.0.9

10 months ago

1.0.8

10 months ago

1.0.7

10 months ago

1.0.6

10 months ago

1.0.5

10 months ago

1.0.4

10 months ago

1.0.3

10 months ago

1.0.2

10 months ago

1.0.1

10 months ago

1.0.0

10 months ago