TypeError:无法读取未定义的属性“49”;React 中的 MachineTyping

我有这个组件的问题。我试图将它从 Vanilla JavaScript 转换为完美的。但那不是那么多。我知道我多次使用 setState() 方法有问题,但我无法弄清楚,究竟是什么。有人可以帮我让它充分发挥作用吗?



class AnimationTypingText extends Component {

    state = {

        text: '',

        indexText: 0,

        activeLetter: 0,

        activeText: 0,

    }


    idInterval = setInterval(() => this.addLetter(), 50);


    addLetter = () => {


        const txt = [

            'HTML Lorem ipsum dolor sit amet consectetur',

            'CSS adipisicing elit. Repellat, laudantium dolorum',

            'JavaScript consequuntur dicta esse soluta aut assumenda', 

            'React error iusto qui atque necessitatibus ', 

            'Node tempore dolor laborum.', 

            'MongoDB Aspernatur quam voluptatum quis possimus.'];     


        if ( this.state.activeLetter >= 0 ) {

            this.setState({

                text: this.state.text + txt[this.state.activeText][this.state.activeLetter],

            });

        }

        this.setState({

            activeLetter: this.state.activeLetter + 1,

        });

            console.log('check 1')

        if ( this.state.activeLetter === txt[this.state.activeText].length ) {


            console.log('check 2')

            this.setState({

                activeText: this.state.activeText + 1,

            })

            if ( this.state.activeText === txt.length ) return; 



                return setTimeout(() => {

                    this.setState({

                        activeLetter: 0,

                        text: '',

                    })

                }, 2000)

        }

        setTimeout(this.idInterval, 100);

    }


    render() {

        return(

            <div>

                <div onLoad={this.idInterval}>

                    <p>{this.state.text}</p>

                </div>

            </div>

        )

    }

}



白衣染霜花
浏览 158回答 1
1回答

ITMISS

有趣的问题!我的建议是编写一个函数来知道该行中是否有未来的字符,另一个来显示未来的行(如果我们在最后一行,则返回第 0 行)。另外,请注意. setState如果您当前构建的状态依赖于先例状态,则需要使用以下语法:setState(previousState => { ... })你可以试试我的解决方案:import React from "react";class AnimationTypingText extends React.Component {&nbsp; state = {&nbsp; &nbsp; text: "",&nbsp; &nbsp; indexText: 0,&nbsp; &nbsp; activeLetter: -1,&nbsp; &nbsp; activeText: 0&nbsp; };&nbsp; idInterval = setInterval(() => this.addLetter(), 50);&nbsp; addLetter = () => {&nbsp; &nbsp; const txt = [&nbsp; &nbsp; &nbsp; "HTML Lorem ipsum dolor sit amet consectetur",&nbsp; &nbsp; &nbsp; "CSS adipisicing elit. Repellat, laudantium dolorum",&nbsp; &nbsp; &nbsp; "JavaScript consequuntur dicta esse soluta aut assumenda",&nbsp; &nbsp; &nbsp; "React error iusto qui atque necessitatibus ",&nbsp; &nbsp; &nbsp; "Node tempore dolor laborum.",&nbsp; &nbsp; &nbsp; "MongoDB Aspernatur quam voluptatum quis possimus."&nbsp; &nbsp; ];&nbsp; &nbsp; const getNextLine = () => {&nbsp; &nbsp; &nbsp; if (this.state.activeText >= txt.length - 1) {&nbsp; &nbsp; &nbsp; &nbsp; return 0;&nbsp; &nbsp; &nbsp; } else return this.state.activeText + 1;&nbsp; &nbsp; };&nbsp; &nbsp; const hasNextChar = () => {&nbsp; &nbsp; &nbsp; if (this.state.activeLetter < txt[this.state.activeText].length - 1) {&nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; &nbsp; } else return false;&nbsp; &nbsp; };&nbsp; &nbsp; if (hasNextChar()) {&nbsp; &nbsp; &nbsp; this.setState(previousState => ({&nbsp; &nbsp; &nbsp; &nbsp; text:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; previousState.text +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; txt[previousState.activeText][previousState.activeLetter + 1],&nbsp; &nbsp; &nbsp; &nbsp; activeLetter: previousState.activeLetter + 1&nbsp; &nbsp; &nbsp; }));&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; const nextLine = getNextLine();&nbsp; &nbsp; &nbsp; this.setState({&nbsp; &nbsp; &nbsp; &nbsp; activeText: nextLine,&nbsp; &nbsp; &nbsp; &nbsp; text: "",&nbsp; &nbsp; &nbsp; &nbsp; activeLetter: -1&nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; }&nbsp; };&nbsp; render() {&nbsp; &nbsp; return (&nbsp; &nbsp; &nbsp; <div>&nbsp; &nbsp; &nbsp; &nbsp; <div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <p>{this.state.text}</p>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; );&nbsp; }}export default AnimationTypingText;最后一件事,如果您不想像我的解决方案那样永远运行,请小心清除componentWillUnmount方法中的间隔。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript