I defined a state variable named fullText
.
fullText
The default value is false
.
When the full text is clicked, I want to invert my state value and enable the text to close and open. But when clicked, the text of all rows in the table opens, how can I make it row-specific?
{ !this.state.fullText ? <div onClick={() => this.setState({ fullText: !this.state.fullText })} className="text-primary cursor-pointer font-size-14 mt-2 px-2" key={props.data?.id}> Full Text </div> : <> <div onClick={() => this.setState({ fullText: !this.state.fullText })}className="text-primary cursor-pointer font-size-14 mt-2 px-2" key={props.data?.id}> Short Text </div> <span>{ props.data?.caption}</span> </> }
It seems that the code example in the question is repeated for every line, but only 1 state
fullText
(showMore
in CodeSandbox) is common to all of them. So they all turn on or off together.If you want each row to have separate open/close functionality, then you need 1 such state per row. A simple solution might be to embed this status into each row's data: