1
votes

I'm trying to render some child components that contain some collapsibles using MateralizeCss, and inside each collapsible three tabs are present, and on each tab there is a button also each of the collapsible headers is assigned a name that I pass through the props.

now on OnClick event of each of those buttons, I call a function which updates the state of the child component with the tab which is selected ('easy', 'med' or hard), something like this.setState({active: this.button.parentname}) and return the name of that collapsible to the parent, but the problem is that the returned name is not the correct one, it is of the first child which was first rendered.

to understand this, suppose I have state in my parent component like.

{category: [

    {"name": 1, .. some other properties}, // data of first collapsible 
    {"name": 2, .. some other properties}, // second

]}

Initially, the state is empty but later the category is being filled asynchronously, but I've handled that.

so I render it like this,

<ul className="collapsible">
     {this.state.category.length > 0 ? (this.state.category.map(((category, key) => (
    <CategoryCollapsible key={key} category={category} get_name={this.get_name} />
   )))) : null}
</ul>

and on child component side I just have the <li> tag inside which the collapsible headers' name is this.props.category.name.

and in the body I just have tabs with button with onClick as I defined above, i.e. return the header name of the collapsible.

so, what could be the reason for this? why is react being confused between the two rendered child components?

I also checked the props using ComponentDidMount() and the props are correct, but as soon as the onClick event is happening the props are just changed to the first one? what is this sorcery? any guesses?

1
Would you mind supplying some pseudo code in a CodePen or CodeSandbox? - Matt Oestreich
ok, I'll put it in a minute. - P.hunter
Sweet!.. Thanks - Matt Oestreich
@MattOestreich I tried to replicate My issue on CodeSandbox but looks like materalize is causing an issue of crossorigins codesandbox.io/s/boz91?fontsize=14 here is the link - P.hunter
Glad you were able to get it fixed! Thanks for the update - Matt Oestreich

1 Answers

1
votes

Alright, I fixed it, actually each of the collapsible needed a unique ID, so I just put the name as the id of the collapsible.

to conclude we can deduce that each functionality (element) child component must have a unique id's to identify them for any function to identify them.