JavaScript React.js render subcomponents from component react render sub component,react force render subcomponent

I want to create a reusable component where the DOM structure can be different each time the component is rendered. Let's say I have this

class Comp extends React.Component {
    constructor() {
        super()
        this.state = {
            click: null,
        }
    }
    render() {
        return(
            <div>
                {this.props.chidren}
            </div>
        )
    }
    handleButton1() {
        this.setState({click: 'button1'});
    }
    handleButton2() {
        this.setState({click: 'button2'});
    }
}
class SubComp1 extends React.Component {
    render() {
        return(
            <button onClick={() => this.props.handleButton1()}>Button 1</button>
        )
    }
}
class SubComp2 extends React.Component {
    render() {
        return (
            <button onClick={() => this.props.handleButton2()}>Button 2</button>
        )
    }
}
ReactDOM.render((
<Comp>
    <div id="somediv">
        <div id="andanother">
            <SubComp1 />
        </div>
    </div>
    <div id="andanotherother">
        <SubComp2 />
    </div>
</Comp>), document.getElementById('app'))

Currently, the two subcomponents do not have access to their respective handler functions. What's the best way of passing the functions handleButton1 and handleButton2 to the subcomponents assuming that their position in the DOM is dynamic and might change depending on the layout of the page. I have thought of 2 solutions so far:

  1. Iterating inside the props.children until I find the element of interest then clone it with the property

  2. Using ref and somehow render the subcomponents after the main component has been rendered through the componentDidMount callback.

What are your thoughts on this?

Answer:1

Why not do something like this:

class Comp extends React.Component {
    constructor() {
        super()
        this.state = {
            click: null,
        }
    }
    render() {
        return(
            <div>
                {this.props.chidren}
            </div>
        )
    }
    handleButton(button) {
        this.setState({click: button});
    }
}

Then in the subcomponents you can do something like

class SubComp1 extends React.Component {
    render() {
        return(
            <button onClick={() => this.props.handleButton('button1')}>Button 1</button>
        )
    }
}

class SubComp2 extends React.Component {
    render() {
        return (
            <button onClick={() => this.props.handleButton('button2')}>Button 2</button>
        )
    }
}
Answer:2

I want to upload an image to the server using Ajax, but there is a problem. Would somebody please help me what is wrong here. I could submit the image using submit form but not ajax. here is my code: ...

I want to upload an image to the server using Ajax, but there is a problem. Would somebody please help me what is wrong here. I could submit the image using submit form but not ajax. here is my code: ...

  1. upload image using javascript
  2. upload image using postman
  3. upload image using jquery ajax
  4. upload image using ajax without form
  5. upload image using retrofit android example
  6. upload image using selenium webdriver
  7. upload image using php
  8. upload image using retrofit android
  9. upload image using jquery
  10. upload image using volley library in android
  11. upload image using curl php
  12. upload image using html
  13. upload image using ajax in laravel
  14. upload image using ajax in php
  15. upload image using laravel
  16. upload image using web api c#
  17. upload image using codeigniter
  18. upload image using axios
  19. upload image using node js
  20. upload image using angular 8

trying to create new date and set it to UTC ISO midnight and get output as following "2017-07-12T00:00:00" but no luck so far. var dateNow = new Date(); var dateToUTC = dateNow.setUTCHours(0,0,0,0); /...

trying to create new date and set it to UTC ISO midnight and get output as following "2017-07-12T00:00:00" but no luck so far. var dateNow = new Date(); var dateToUTC = dateNow.setUTCHours(0,0,0,0); /...

  1. convert date to midnight sql
  2. convert date to midnight java
  3. convert date to midnight sql server
  4. convert date to midnight
  5. oracle convert date to midnight
  6. python convert date to midnight

I have a string in which there are continous occurances of a font tag <font color="blue">DATA ENTRY</font> and in some cases like this <font class="beat">DATA ENTRY</font>...

I have a string in which there are continous occurances of a font tag <font color="blue">DATA ENTRY</font> and in some cases like this <font class="beat">DATA ENTRY</font>...

  1. replace html jquery
  2. replace html element
  3. replace html javascript
  4. replace html tags
  5. replace html tags regex
  6. replace html text jquery
  7. replace html table with div
  8. replace html with ajax response
  9. replace html tags with space
  10. replace html characters javascript
  11. replace html in div jquery
  12. replace html with css
  13. replace html tags in sql
  14. replace html special characters
  15. replace html using javascript
  16. replace html text javascript
  17. replace html with another html
  18. replace html entities javascript
  19. replace html imports with es modules
  20. replace html tables with css div

I'm a beginner at javascript and just getting started with working with the DOM. After grabbing a bunch of spans with different ID names at several different places and grabbing the data from an ...

I'm a beginner at javascript and just getting started with working with the DOM. After grabbing a bunch of spans with different ID names at several different places and grabbing the data from an ...