P.Macs
P.Macs

Reputation: 77

How to know if the props already exists ReactJS?

I'm new to reactjs and I am trying to not overwrite files that is already existed on reactjs. But I really don't know hot to declare if the files already existed.

I've already searched the data by using get props document/search.

This is where I add it on the api

   creater(form) {
        debugger
        var name =  form.something.length;
        for (var i = 0; i < name; i++) {
        form.State = 1;
        if (form.Name already exist){
        form.Name=(form.something[i] + "(1)");
        this.props.poster('document/post', form);
        }
        }
    }

Upvotes: 0

Views: 171

Answers (1)

Rodius
Rodius

Reputation: 2311

Where are you storing the names? You need to store the already existing names somewhere and check if they exist before using the new ones.

Let's say you have an array storing all names. You could do something like this:

creater(form) {
    const names = ['Peter', 'Ben', 'Alice', 'Robert'];

    for (let i = 0; i < form.names.length; i++) { // Check each name in form and change if necessary
      if (names.indexOf(form.names[i]) > -1) { // Already exists
        form.names[i] = form.names[i]+'1';
      }
    }
    this.props.poster('document/post', form);
  }

Upvotes: 1

Related Questions