Stack Exchange
Could not check the input value properly using React.js
I need to check the user input value using React.js but its not working as expected. I am explaining my code below.
   import React, { Component } from "react";
import TodoItems from "./TodoItems";
import "./TodoList.css";
class TodoList extends Component {
  constructor(props, context){
    super(props, context);
    this.state={
      items:[]
    }
    this.listOfKeyWords = ["script", "embed", "object", "iframe"];
    this.addItem=this.addItem.bind(this);
    this.deleteItem = this.deleteItem.bind(this);
    this.editItem = this.editItem.bind(this);
  }
  addItem(e){
    e.preventDefault();
    if(this.state.editKey){
      this.saveEditedText();
      return;
    }
    let maliciousStr = this.listOfKeyWords.find(tag => {
      return this.inputElement.value.toLowerCase().indexOf(tag) !== -1;
    });
    if (!maliciousStr) {
      var itemArray = this.state.items;
      if (this.inputElement.value !== '') {
        itemArray.unshift({
          text:this.inputElement.value,
          key:Date.now()
        })
        this.setState({
          items:itemArray
        })
        this.divRef.insertAdjacentHTML("beforeend", '<p className="textcolor">'+this.inputElement.value+' has added successfully</p>');
        this.inputElement.value='';
        setTimeout( () => {
            this.divRef.querySelector(':last-child').remove();
        }, 3000);
      }
    }else{
      this.inputElement.value='';
    }
  }
  deleteItem(key) {
    const result = window.confirm('Are you sure to delete this item');
    if (result) {
      var filteredItems = this.state.items.filter(function (item) {
        return (item.key !== key);
      });

      this.setState({
        items: filteredItems
      });
    }
  }
  editItem(key){
    this.state.items.map(item =>{
      if (item.key==key) {
        this.inputElement.value=item.text;
      }
    })
    this.setState({editKey: key});
  }
  saveEditedText(){
    let value = this.inputElement.value;
    this.setState(prevState => ({
      items: prevState.items.map(el => {
        if(el.key == prevState.editKey)
          return Object.assign({}, el, {text: value});
         return el;
      }),
      editKey: ''
    }));
    let maliciousStr = this.listOfKeyWords.find(tag => {
      return this.inputElement.value.toLowerCase().indexOf(tag) !== -1;
    });
    if (!maliciousStr) {
      this.divRef.insertAdjacentHTML("beforeend", '<p className="textcolor">'+this.inputElement.value+' has updated successfully</p>');
      setTimeout( () => {
          this.divRef.querySelector(':last-child').remove();
      }, 3000);
      this.inputElement.value='';
    }else{
      this.inputElement.value='';
    }
  }
  render() {
    return (
      <div className="todoListMain">
        <div className="header" id="parentDiv">
          <div className="pageHeading" dangerouslySetInnerHTML={{ __html: "Todo Demo Application" }}></div>
          <div className="wrapper">
            <div ref={divEl => {
            this.divRef = divEl;
          }}></div>
            <form onSubmit={this.addItem}>
              <input ref={(a)=>this.inputElement=a} placeholder="enter task">
              </input>
              <button type="submit">{this.state.editKey? "Update": "Add"}</button>
            </form>
            <TodoItems entries={this.state.items} delete={this.deleteItem} edit={this.editItem}/>
          </div>
        </div>
      </div>
    );
  }
}

export default TodoList;

Here I am adding data and after submitting the message is displaying on the top. Here I have one validation if user input has value like <script>Hii</script> the the message will not display or the value can not be added but in my coding its not happening like this. I need if <script>Hii</script> is there those will be filtered out.

Can you post your actual code so we can see the whole thing

@Deryck : I posted all the code.

what happens when you change var itemArray = this.state.items; to var itemArray = this.state.items.slice();?

Where I have written var itemArray = this.state.items.slice();.
