Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

208
Views
Button click changes div content according to some params

I have the code below, what i want it to do is: when i click the quizzes button , the div with renderComponent in it renders the list of quizzes as in QuizList function, when i press Members button i want the same div to render the list of members as in MemberList function, but i can't manage to do it, could you help me with it?

class GroupPage extends React.Component {
    constructor(props){
        super(props);
        this.state={
            members:[],
            quizzes:[]
        }

    }

    QuizList = () =>{
        const {quizzes} = this.state;
        return(
            <div>{
                
                quizzes.map(({id,...otherQuizProps}) => (
                            <QuizItem key={id} id={id}{...otherQuizProps}/>
                        ))
                }
            </div>
        )
    }
    MemberList = () =>{
        const {members} = this.state;
        return(
            <div>{
                members.map(({id,...otherMemberProps}) => (
                            <GroupMember key={id} id={id}{...otherMemberProps}/>
                        ))
                }
            </div>
        )
    }
    
    renderComponent = (component) =>{
        switch(component){
          case "QuizList":
            return <this.QuizList/>
          case "MemberList":
            return <this.MemberList/>
          default:
            return <this.QuizList/>
        }
      }

    

    render(){
        const {quizzes,members} = this.state;
    return(
        <div className='group-page'>
            <div className='group-controls'>
                <div className='group-buttons'>
                    <button onClick={}>Quizzes</button>
                    <button onClick={}>Members</button>
                </div>
            </div>
            <div>
                {this.renderComponent()};
            </div>
        </div>
    )}
};
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

In the render method you don't provide any parameter to renderComponent. So everytime the default case will be returned, because the parameter component is undefined.

I would store the current selection inside the state of the component and set it with the buttons accordingly.

    constructor(props){
        super(props);
        this.state={
            members:[],
            quizzes:[],
            currentMode: 'QuizList',
        }
    }
...

    render(){
        const {quizzes,members, currentMode} = this.state;
    return(
        <div className='group-page'>
            <div className='group-controls'>
                <div className='group-buttons'>
                    <button onClick={()=>this.setState({currentMode:'QuizList'}) }>Quizzes</button>
                    <button onClick={()=>this.setState({currentMode:'MemberList'}) }>Members</button>
                </div>
            </div>
            <div>
                {this.renderComponent(currentMode)};
            </div>
        </div>
    )}
    }
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!