Git Product home page Git Product logo

lac617a / react-switch-casu Goto Github PK

View Code? Open in Web Editor NEW
2.0 1.0 0.0 16 KB

Conditional rendering in React works the same way conditions work in JavaScript. Stop using JavaScript operators like if or ternary conditional operators to create elements that represent the current state and let `react-switch-casu` take care of updating the UI to match them.

License: MIT License

JavaScript 8.38% HTML 12.80% TypeScript 70.04% CSS 8.78%
conditional-rendering react reactjs switch switch-case ternary-operator

react-switch-casu's Introduction

Simplify conditional rendering in react

Version Downloads GitHub stars gzip size

MIT License

Demo

Take a look at the demo

Installing

Package manager

$ npm install react-switch-casu

Using yarn:

$ yarn add react-switch-casu

Using pnpm:

$ pnpm add react-switch-casu

What is it, and what it does?

It is a simple package that provides multiple utility components for simplifying conditional rendering in react. By using this package you can make your code more readable and maintainable as shown below.

  1. Switch
    • Wrapper component for Case and Default.
    • It takes an expression as a prop.
    • It also takes two optional boolean props fallthrough and enableMemo.
    • By default both 'fallthrough' and 'enableMemo' are false.
    • After enabling memo feature, it will only re-render if the expression changes.
    • After enabling fallthrough you will need to pass a boolean break in the Case component wherever you want to stop fallthrough.
    • Anything else outside of Case and Default will be ignored.
  2. Case
    • Must be used inside Switch.
    • It takes a conditional as a prop.
    • Must contain children/child.
    • Optional boolean prop break if fallthrough is enabled in Switch.
  3. Default
    • Must be used inside Switch.
    • Must contain children/child.
import React from "react";
import Switch from "react-switch-casu";

function SwitchCaseSimpleExample() {
  const [show, setShow] = useState(false)
  return (
    <Switch>
      <Switch.Case condition={show}>
        {/* here your login component  */}
      </Switch.Case>
      <Switch.Case condition={!show}>
        {/* here your register component  */}
      </Switch.Case>
      <Switch.Default>
        <h1>Welcome to Home</h1>
      </Switch.Default>
    </Switch>
  );
}

This is the basic syntax for a Switch statement with the expression condition

import React from "react";
import Switch from "react-switch-casu";

function SwitchCaseExpressionExample() {
  const [expression, setExpression] = useState(3)
  return (
    <Switch expression={expression}>
      <Switch.Case condition={1}>
        {/* here your login component  */}
      </Switch.Case>
      <Switch.Case condition={2}>
        {/* here your register component  */}
      </Switch.Case>
      <Switch.Default>
        <h1>Welcome to Home</h1>
      </Switch.Default>
    </Switch>
  );
}

The computer will review the Switch statement and check for equality strict === between the case Case and the expression expression.

If one of the cases matches the expression expression, will execute the code inside the Case case clause.

If multiple cases match the Switch statement, the first case Case that matches the expression expression.

If none of the cases match the expression, the clause will be executed default Default.

With the Switch statements they can have a cleaner syntax than the complicated ternary or if else statements, which are sometimes a little cumbersome to understand.

Switch fallthrough example

import React from "react";
import Switch from "react-switch-casu";

function SwitchCaseExpressionExample() {
  const [weekDayNo, setWeekDayNo] = useState(3)
  return (
    <Switch expression={weekDayNo} fallthrough>
      <Switch.Case condition={1}>Monday</Switch.Case>
      <Switch.Case condition={2}>Tuesday</Switch.Case>
      <Switch.Case condition={3}>Wednesday</Switch.Case>
      <Switch.Case condition={4}>Thursday</Switch.Case>
      <Switch.Case condition={5}>Friday</Switch.Case>
      <Switch.Case condition={6}>Saturday</Switch.Case>
      <Switch.Case condition={7} break>Sunday</Switch.Case>
      <Switch.Default>
        week day number must be between 1 and 7
      </Switch.Default>
    </Switch>
  );
}

The current syntax for something like this could look like this

return (
  <div>
    {(() => {
      switch (condition) {
        case maybeMatchesCondition:
          return <h1>I match</h1>;
        case alsoMaybeMatchesCondition:
          return <h1>No, I match</h1>;
        default:
          return <h1>Oh no, nothing matched!</h1>;
      }
    })()}
  </div>
)

MIT License

react-switch-casu's People

Contributors

lac617a avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.