0

I have a child component which i am looping over array to print out title and values. I have an event listener which renders a new row of title and values. I have a button in child component which i want do not want to be displayed by default but rendered only when i add new rows. So every 2 rows, there will be one button, for every 3, there will be 2 and so on.

This is my app.js file

import React, {Component} from 'react';
import './App.css';
import Child from './Child.js'

class App extends Component {
   state = {
      myArray: [
       { title: 'Hello', value: 'hello' }
     ]
   }

 addNewField = () => {
    const myArray = [...this.state.myArray, { title: 'Hello', value: 'hello' }]
     this.setState ({
    myArray
  })
}

render() {
  return (
    <div className = "App">
   {
    this.state.myArray.map ( (val,idx) => {
      return (
        <Child
        key = {idx}
        title = {val.title}
        animal = { val.value }
        />
      )
    })
  }
  <button onClick = {this.addNewField}>Add Me</button>
</div>
   )
 }
}
export default App;

This is the setup for my Child Component:-

import React from 'react'

const Child = (props) => {
  return (
     <div>
        <h3>{props.title}</h3>
        <h4>{props.value}</h4>
        <button>New Block!!</button>
    </div>
   )
}

 export default Child

So basically the button in the Child component named new block should not be displayed by default but only after every click there after. Thank you.

2
  • Expose a property on the Child component to control the rendering Commented Jan 30, 2020 at 0:23
  • hmm i tried to do it with index but that didn't work. Commented Jan 30, 2020 at 0:29

1 Answer 1

1

Add a prop to the parent with the index of the map loop. Then add a flag so only children rendered after the first get the "New Block!!" button:

render() {
  return (
    <div className = "App">
   {
    this.state.myArray.map ( (val,idx) => {
      return (
        <Child
        key = {idx}
        title = {val.title}
        animal = { val.value }
        renderIndex = {idx}
        />
      )
    })
  }
  <button onClick = {this.addNewField}>Add Me</button>
</div>
   )
 }
}
import React from 'react'

const Child = (props) => {
  return (
     <div>
        <h3>{props.title}</h3>
        <h4>{props.value}</h4>
        {props.renderIndex > 0 && <button>New Block!!</button>}
    </div>
   )
}
export default Child
Sign up to request clarification or add additional context in comments.

2 Comments

@Somethingwhatever Sorry! Just tested and you can't use the key prop like this. You need to pass down the index on another prop. I've edited my answer to reflect this.
Awesome!! yeah i have ran into this in the past. it's something to do with the key itself. using the renderIndex worked. Thank you

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.