简体   繁体   English

当状态改变时,React-typing-animation 不会重新渲染

[英]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 = ["this is a test", "this is another test"];

  updateDisplayedInfo() {
    if (this.state.currentIndex >= this.infos.length) {
      this.setState({
        currentInfo: this.infos[0],
        currentInfo: 0,
      });
    } else {
      this.setState(prevState => ({
        currentIndex: prevState.currentIndex + 1,
        currentInfo: this.infos[prevState.currentIndex + 1],
      }));
    }
  }

  constructor(props) {
    super(props);
    this.state = {
      currentInfo: this.infos[0],
      currentIndex: 0,
    };

    this.updateDisplayedInfo = this.updateDisplayedInfo.bind(this);
  }

  render() {
    return (
      <Typing onFinishedTyping={this.updateDisplayedInfo}>
        {this.state.currentInfo}
      </Typing>
    );
  }
}

export default InfoDisplayer;

I'm using https://github.com/notadamking/react-typing-animation which is a component used for getting a text typing animation.我正在使用https://github.com/notadamking/react-typing-animation这是一个用于获取文本输入动画的组件。 It has a handler called onFinishedTyping which can be used to do something after the typing is done.它有一个名为onFinishedTyping的处理程序,可用于在输入完成后执行某些操作。 I'm using it to change my component state to update the current info state.我正在使用它来更改我的组件状态以更新当前信息状态。

Although that updateDisplayedInfo is called and currentInfo is updated, the component is not rendered again.尽管调用了updateDisplayedInfo并更新了currentInfo ,但不会再次呈现该组件。

Why?为什么? I believe setState should re-render the component.我相信setState应该重新渲染组件。

Addition : online code补充:在线代码
编辑epic-surf-b7pgy

Thanks to https://stackoverflow.com/users/11872246/keikai edit, you can use the react dev tools to see that the state has been changed after the first typing animation感谢https://stackoverflow.com/users/11872246/keikai编辑,可以使用react dev工具查看第一次打字动画后状态已经改变

Some notice points:一些注意点:

  • Add loop添加循环
  • Add Typing.Reset添加Typing.Reset

Refer to document 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);

编辑兴高采烈的爱因斯坦 ncc7q

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM