0
render() {
   const Array = this.props.students.map((data) => {
      return (
         <button 
           key={data.name}
           //key={data.birthDate}
           //key={data.age}
           //key={data.class}
         >{data.name}</button>
      );
   });

   return (
     <div>
       {Array}
       <StudentInfo name = {"Amira"}/>
     </div> 
   );
}

This is my code. Currently I'm trying to pass array data from that <button></button> into <StudentInfo /> and replace the name = {"Amira"} Can anyone help me with this?

Thank you.

4
  • Please be a little more elaborate as to what you're asking. Commented Jan 26, 2018 at 1:00
  • Actually I want to do something like this <StudentInfo name = {data.name}/> is it possible? I want the data from the button.{data.name} to be pass to <StudentInfo> but i got error when do this. Commented Jan 26, 2018 at 1:15
  • Okay, so you want a single data.name or a complete array? Commented Jan 26, 2018 at 1:22
  • the complete array Commented Jan 26, 2018 at 1:32

3 Answers 3

1

this might be your answer: How to pass an array of items in REACT.js

Sign up to request clarification or add additional context in comments.

4 Comments

and actually I want to do something like this <StudentInfo name = {data.name}/> is it possible?
I want the data from the button.{data.name} but i got error when do this
What error are you facing? Check your browser console log and add it to your question, please.
passing error : unexpected token var and it also said that code is not valid
1

Doing this will solve the issue,

render() {
   const nameArray = [];
   const Array = this.props.students.map((data) => {
      nameArray.push(data.name); //fill the array with names
      return (
         <button 
           key={data.name}
           //key={data.birthDate}
           //key={data.age}
           //key={data.class}
         >{data.name}</button>
      );
   });

   return (
     <div>
       {Array}
       <StudentInfo name = {nameArray}/>
     </div> 
   );
}

2 Comments

But How if I want it when we click on a button, only data about that person will be display?
For a button click, you need to add that logic to your question first by editing it.
0

What you want to do is something like this

constructor(props){
  super(props);
  this.students = [
    {name: 'john'},
    {name: 'paul'},
    {name: 'clara'}
  ];      
  this.state = {
    selectedStudent: this.students[0]
  }
  this.selectStudent = this.selectStudent.bind(this);
}

selectStudent(student){
  this.setState({selectedStudent: student});
}

render(){
  return (
    <div>
      {this.students.map((student, i) => (
        <button key={i}
          onClick={() => this.selectStudent(student)}
        >{student.name}</button>
      ))}
      <h1>{this.state.selectedStudent.name}</h1>
    </div>
  );
}

online demo: https://codesandbox.io/s/91wp5j33pw

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.