0

I have an array licenseKeys in React state, and need to push to it all new values of lisenceKey's that user types to input fields. It works, but when I'm trying to check whether value of new key is present in array this.state.licenseKeys.includes(this.state.licenseKey), I'm getting an error Uncaught TypeError: Cannot read property 'includes' of undefined. I tried to use an arrow function with some instead, but I got the same error Uncaught TypeError: Cannot read property 'some' of undefined. What am I doing wrong here?

 if (localStorage.getItem("formData")) {
  this.state = JSON.parse(localStorage.getItem("formData"));
} else {
  this.state = {
    ppks: [],
    alert: false,
    ipState: "",
    portState: "",
    userNameState: "",
    passwordState: "",
    licenseKey: "",
    licenseKeys: [],
    licenseKeysObject: []
   // refreshButttonDisabled: true
}    
    }

 licenseKeysSetter = () => {
    if (this.state.licenseKey !== "" && this.state.licenseKeys.includes(this.state.licenseKey) === false){          
        this.setState({
            licenseKeys: [ ...this.state.licenseKeys || [], this.state.licenseKey]              
        }, () => {
            console.log(this.state.licenseKeys);            
        });
    }   
  }
.....
<ul>
      {ppkData.map((item, index) => {   
       if (item.model === "8l") {  
        return (
          <div className="ppkCard" key={index + 1}>

          <form>
          <div className="input-group input-group-sm mb-3">
          <div className="input-group-prepend">
            <span className="input-group-text" id="inputGroup-sizing-sm">Enter License Key</span>
          </div>
          <input type="text"
                 className="form-control"
                 placeholder="000-000-000-000-000-000" 
                 onChange={this.licenseKeysHandler}
                 value={this.state.licenseKey || ''} 
                 aria-label="Small" 
                 aria-describedby="inputGroup-sizing-sm" />
        </div>       
        <button type="button" onClick={this.licenseKeysSetter} >Save</button>     
        </form>
.........
 licenseKeysHandler = (event) => {      
    this.setState({licenseKey: event.target.value}, () => {
        console.log(this.state.licenseKey);
    })      
  }
16
  • Can you add the full component code for more context? Commented Nov 21, 2019 at 8:42
  • What is value of that array inside licenseKeysSetter() , please add console.log there and tell. Commented Nov 21, 2019 at 8:43
  • Try this.state.licenseKeyintead of this.state.licenseKey !== "" Commented Nov 21, 2019 at 8:43
  • 2
    If you define your state out of constructor drop this part. just use it as state = ... Commented Nov 21, 2019 at 9:07
  • 1
    this.state = JSON.parse(localStorage.getItem("formData")); is the most likely place that sets this.state.licenseKeys to undefined. can you please console.log(localStorage.getItem("formData"))? Commented Nov 21, 2019 at 9:52

2 Answers 2

1

If localStorage does not include all keys necessary in the state (e.g. after a new version release), you can separate initial state from loading localStorage like this:

class MyComponent() {
  state = {
    ppks: [],
    alert: false,
    ipState: "",
    portState: "",
    userNameState: "",
    passwordState: "",
    licenseKey: "",
    licenseKeys: [],
    licenseKeysObject: []
  }  
  componentDidMount() {
    this.setState(JSON.parse(localStorage.getItem("formData")))   
  }
  ...
}
Sign up to request clarification or add additional context in comments.

Comments

0

I changed code like this, and it works fine for me...

 licenseKeysSetter = () => {
    //console.log(this.state.licenseKey);
    console.log(this.state.licenseKeys);
    //console.log(this.state);
    if (this.state.licenseKeys === undefined) {
        if (this.state.licenseKey !== "" ){         
        this.setState({
            licenseKeys: [ ...this.state.licenseKeys || [], this.state.licenseKey]              
        }, () => {
            console.log(this.state.licenseKeys);            
        });
    } 
  } else {      
    if (this.state.licenseKey !== "" && !this.state.licenseKeys.includes(this.state.licenseKey)){            
        //console.log(this.state.licenseKeys);
        this.setState({
            licenseKeys: [ ...this.state.licenseKeys || [], this.state.licenseKey]              
        }, () => {
            console.log(this.state.licenseKeys);            
        });
    }  
    }   
  }

1 Comment

It's better to identify the actual issue instead of updating the working code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.