r/reactjs • • Apr 03 '18

Beginner's Thread / Easy Questions (April 2018)

Pretty happy to see these threads getting a lot of comments - we had almost 200 comments in last month's thread! If you didn't get a response there, please ask again here!

Soo... Got questions about React or anything else in its ecosystem? Stuck making progress on your app? Ask away! We’re a friendly bunch. No question is too simple.

The Reactiflux chat channels on Discord are another great place to ask for help as well.

17 Upvotes

229 comments sorted by

View all comments

Show parent comment

1

u/pgrizzay Apr 10 '18

I don't think the error is in the code you posted... I put it in a fiddle, and can click on the cards without getting a warning at all (which makes sense): https://jsfiddle.net/69z2wepo/170457/

Are you doing something like:

class InnerCard ... {
  render() {
    return (
      <div>
        {this.props.cardSelected.map(number => <div>{number}</div>)}
      </div>
    )
  }
}

in InnerCard?

1

u/homercrates Apr 10 '18
 const InnerCard = (props) => {
    const newDate = new Date().getTime();
    	// const myInnerOptions = 
    	const myInnerOptions = props.cardSelected.map((cardSelected, index, i) => {
    		return (
    			<div key={cardSelected[i]} onClick={() => this.clickHandler(index)}>
    				card {cardSelected} and index {index}
    			</div>
    		);
    	})  
    	// NEW PROBLEM  i am not getting a unique Key .. this is creating an error every time the array values are identical..  2,2 3,3 1,1
    
    
    	
    	console.log(props.cardSelected, 'here here');
    	//for (let [key, value] of Object.entries())
    
    	return (
    		<div>{myInnerOptions}</div>
    
    	);
    }

1

u/pgrizzay Apr 10 '18

Take a look at the difference between goodInnerOptions and badInnerOptions in this fiddle: https://jsfiddle.net/69z2wepo/170515/

You just need to make a few adjustments to how you're using the .map() function, hope that helps!

1

u/homercrates Apr 10 '18

Sorry had to do a school pickup run. So the difference I am seeing between goodInnerOptions and badInnerOptions is I should drop the i variable and use the index as the key. correct? or did i miss something else in there? .. Oh and making the key= simply index

2

u/pgrizzay Apr 11 '18

Yup, I think you summed it up, good luck!

1

u/homercrates Apr 10 '18

PERFECT. made the changes you suggested in both Components. fully digested it and the theory. And now I am getting no errors or warnings. thank you paul. thank you. (I truly appreciate you taking your time to help.. and further explain)

1

u/homercrates Apr 10 '18

I didn't know you wanted the recode of InnerCard

Its still mesy with my notes of stuff breaking. Although that was fixed. It works now.. it just throws up a warning of unique key's but it works now it renders both <div>'s of an array of 2

1

u/homercrates Apr 10 '18

perhaps is isnt in <Hand />? if you aren't getting an error in JS Fiddle.
hmm. huh.