Skip to content Skip to sidebar Skip to footer

React-typing-animation Is Not Re-rendered When State Is Changed

I have the following component import React, { Component } from 'react'; import Typing from 'react-typing-animation'; export class InfoDisplayer extends Component { infos = ['th

Solution 1:

Some notice points:

  • Add loop
  • Add Typing.Reset

Refer to document here

enter image description here


import ReactDOM from "react-dom";

import "./styles.css";

import React, { Component } from "react";
import Typing from "react-typing-animation";

import "./styles.css";

const infos = ["this is a test", "this is another test"];

export class InfoDisplayer extends Component {
  constructor(props) {
    super(props);
    this.state = {
      currentIndex: 0
    };
  }

  componentDidUpdate() {
    console.log(this.state.currentIndex);
  }

  updateDisplayedInfo = () => {
    this.setState({ currentIndex: this.state.currentIndex === 0 ? 1 : 0 });
  };

  render() {
    return (
      <Typing onFinishedTyping={this.updateDisplayedInfo} loop>
        {infos[this.state.currentIndex]}
        <Typing.Reset count={1} delay={500} />
      </Typing>
    );
  }
}

export default InfoDisplayer;

function App() {
  return (
    <div className="App">
      <h1>Hello CodeSandbox</h1>
      <InfoDisplayer />
    </div>
  );
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

Edit elated-einstein-ncc7q


Post a Comment for "React-typing-animation Is Not Re-rendered When State Is Changed"