Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

infinite typing effect react

const { render } = ReactDOM;
const { useState, useEffect } = React;

const CONSTANTS = {
  DELETING_SPEED: 30,
  TYPING_SPEED: 150,
}

function TypeWriter({ messages, heading }) {
  const [state, setState] = useState({
    text: "",
    message: "",
    isDeleting: false,
    loopNum: 0,
    typingSpeed: CONSTANTS.TYPING_SPEED,
  });

  useEffect(() => {
    let timer = "";
    const handleType = () => {
      setState(cs => ({
        ...cs, // cs means currentState
        text: getCurrentText(cs),
        typingSpeed: getTypingSpeed(cs)
      }));
      timer = setTimeout(handleType, state.typingSpeed);
    };
    handleType();
    return () => clearTimeout(timer);
  }, [state.isDeleting]);

  useEffect(() => {
    if (!state.isDeleting && state.text === state.message) {
      setTimeout(() => {
        setState(cs => ({
          ...cs,
          isDeleting: true
        }))
      }, 500);
    } else if (state.isDeleting && state.text === "") {
      setState(cs => ({
        ...cs, // cs means currentState
        isDeleting: false,
        loopNum: cs.loopNum + 1,
        message: getMessage(cs, messages)
      }));
    }
  }, [state.text, state.message, state.isDeleting, messages]);

  function getCurrentText(currentState) {
    return currentState.isDeleting
      ? currentState.message.substring(0, currentState.text.length - 1)
      : currentState.message.substring(0, currentState.text.length + 1);
  }

  function getMessage(currentState, data) {
    return data[Number(currentState.loopNum) % Number(data.length)];
  }

  function getTypingSpeed(currentState) {
    return currentState.isDeleting
      ? CONSTANTS.TYPING_SPEED
      : CONSTANTS.DELETING_SPEED;
  }

  return (
    <h1>
      {heading}&nbsp;
        <span>{state.text}</span>
      <span id="cursor" />
    </h1>
  );
}

let msgs = ["WELCOME TO MY WORLD","THIS IS MY WEBSITE","I AM AT YOUR SERVICE"];
render(<TypeWriter heading={"Things I want to type:"} messages={msgs} />, document.body);
Comment

infinite typing effect react

@import url('https://fonts.googleapis.com/css?family=VT323');
body {
  font-family: 'VT323', monospace;
  background-color: #003B00;
  padding: 1em 2em;
}

h1 {
  color: #00FF41;
}

#cursor {
  border-left: .1em solid #00FF41;
  animation: blink .7s steps(1) infinite;
}

@keyframes blink {
  50% {
    border-color: transparent;
  }
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: Google Web App Script Unknown Parameter Error on Load 
Javascript :: React.js setState on page load not working, how to fix 
Javascript :: javascript For some reason my content within an array is not printing out to the screen 
Javascript :: javascript vuelidate identical passwords only if checkbox is ticked 
Javascript :: -1 in js 
Javascript :: How to pass variables from one page to another with AngularJS 
Javascript :: how to know the number of eventlisteners in javascript 
Javascript :: angularjs No alignment and missing item in a vertical menu 
Javascript :: DeepCopy in Angularjs 
Javascript :: Prevent the wiping of an Array after routing Angular.js 
Javascript :: Wait for AngularJS Service to finish running 
Javascript :: Relaxed "angularjs" style expression parsing missing in vue 
Javascript :: Using useEffect with async 
Javascript :: context Menus 
Javascript :: ERROR Error: Uncaught (in promise): NullInjectorError: R3InjectorError(AuthModule)[AuthService - AuthService - AngularFirestore - InjectionToken 
Javascript :: PAN SNAP 
Javascript :: error first callback in node js 
Javascript :: nextjs app wdyr 
Javascript :: using parseint in javascript 
Javascript :: chat v2 msg and time good 
Javascript :: short-circuit evaluation , use of || operator 
Javascript :: Accessing Our CryptoCurrency blockchain through local server 
Javascript :: removevalidators angular 
Javascript :: querySelectorAll select multiple element types 
Javascript :: Calculator for two numbers 
Javascript :: regex specific number of characters 
Javascript :: JavaScript HTMLCollection Object 
Javascript :: get images from mysql with php jquery ajax and display them in html page inside DIVs 
Javascript :: Backbone Collection 
Javascript :: How many options are there to climb a ladder with N 
ADD CONTENT
Topic
Content
Source link
Name
1+7 =