I have such js code:
let idCounter = 0;
export default class TestComponent extends Component {
constructor(props) {
super(props);
this.uniqueId = `TestComponent-${idCounter++}`;
}
render() {
return (<div id={this.uniqueId> Some content </div>);
}
}
I need to have unique Id for every rendered element, so I count them. But I have SSR with Next.js. When the page initializes on the server then idCounter is incremented few times because of server rendering.
Then normal browser tries to render this component starting idCounter from 0 and replacing server-side rendered id (e.g. "TestComponent-5" to "TestComponent-0"). And I end up with error in console:
"Warning: Prop
id
did not match. Server: "TestComponent-5" Client: "TestComponent-0" "
I don't know how to handle that. Any ideas?