Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
257 views
in Technique[技术] by (71.8m points)

javascript - 1 All instances of a component reference the first instance instead of itself; 2 Children props get updated only after calling parent's setState twice

UPDATE: I easily fixed the first issue by providing unique names for all the 'Rating' components, but the question about 2 x setState fixing props remains open.

I actually have two questions. The second one emerged from trying to solve the first one. The initial problem was that 'this' in each of the onChange Rating's property in each of the SongScoring components pointed to the first instance of the SongScoring class instead of itself:

Parent code:

import React, { Component } from 'react';
import './Assess.css';
import { Accordion } from 'react-bootstrap';
import SongScoring from './SongScoring';

class Assess extends Component {
  constructor(props) {
    super(props);

    const songs = [
      {
        track: "song1",
      },
      {
        track: "song2",
      },
      {
        track: "song3",
      },
      {
        track: "song4",
      },
    ]

    this.state = {
      songs: songs,
    };
  }

  render() {
    return (
      <div className="root">
        <Accordion defaultActiveKey="0">
          {
            this.state.songs.map((song, index) => (
              <SongScoring song={song} key={index.toString()} index={index} />
            ))
          }
        </Accordion>
      </div>
    );
  }
}

export default Assess;
question from:https://stackoverflow.com/questions/65912738/1-all-instances-of-a-component-reference-the-first-instance-instead-of-itself-2

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...