简体   繁体   English

为什么我的 updatedText 组件没有被渲染?

[英]Why my updatedText component isn't being rendered?

I'm trying to complete a challenge and those are the final steps:我正在尝试完成一项挑战,这些是最后的步骤:

5 -> Render a list of CharComponents where each CharComponent receives a different letter of the entered text (in the initial input field) as a prop. 5 -> 渲染一个 CharComponents 列表,其中每个 CharComponent 接收输入文本的不同字母(在初始输入字段中)作为道具。

6 -> When you click a CharComponent, it should be removed from the entered text. 6 -> 当您单击一个 CharComponent 时,它应该从输入的文本中删除。

Step 5 goes fine but I can't realize why the updated textList isn't rendered.第 5 步运行良好,但我不明白为什么没有呈现更新的 textList。

App.js:应用程序.js:

import React, { Component } from "react";
import "./App.css";

import Validation from "./ValidationComponent";
import Char from "./CharComponent";

class App extends Component {
state = {
    text: "",
    length: 0,
};

textHandler = (e) => {
    const text = e.target.value;
    const length = text.length;
    this.setState({ text: text, length: length });
};

deleteCharHandler = (index) => {
    const text = this.state.text.split("");
    text.slice(index, 1);
    const updatedText = text.join("");
    this.setState({ text: updatedText });
};
render() {
    const textList = this.state.text.split("").map((char, index) => {
    return (
        <Char
        letter={char}
        key={index}
        click={() => this.deleteCharHandler(index)}
        />
    );
    });

    return (
    <div className="App">
        <input type="text" onChange={this.textHandler} />
        <p>{this.state.length}</p>
        <Validation length={this.state.length} />
        {textList}
    </div>
    );
}
}

export default App;

CharComponent:字符组件:

import React from "react";

import "./styles.css";

const charComponent = (props) => {
return (
    <div className="Char">
    <p onClick={props.click}>{props.letter}</p>
    </div>
);
};

export default charComponent;

ValidationComponent:验证组件:

import React from "react";

import "./styles.css";

const validationComponent = (props) => {
let message = "";
if (props.length < 5 && props.length !== 0) message = "Text is too short!!!";
if (props.length > 18) message = "Text is long enough!!!";
return (
    <div>
    <p>{message}</p>
    </div>
);
};

export default validationComponent;

In deleteCharHandler , you are using slice instead of splice .deleteCharHandler中,您使用的是slice而不是splice

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

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