How to change a variable value in react on button click

Rahul

When someone clicks on "Add more" button, I want the pickers variable to change (contain two instances of mainPart.

On page load, there is only one mainPart inside pickers. But I want to change it when someone clicks on "Add more".

I have a react code like this:

export default class TestComponent extends React.Component {
    constructor(props) {
        // Stuff here
    }
  
    mainPart(a,b,c) {
        return (
            // Stuff here
        )
    }
    
    changeMyVariable(a,b,c,type) {
        if (type==1) {
            return [
                ( 
                    <span>
                        mainPart(a,b,c)
                    </span>
                )
            ]
        }
        if (type==2) {
            return [
                ( 
                    <span>
                        mainPart(a,b,c)
                    </span>
                ),
                ( 
                    <span>
                        mainPart(a,b,c)
                    </span>
                )
            ]
        }
    }
    
    render() {
        let pickers = this.changeMyVariable(a, b, c,1);
        
        return (
            {pickers}
            <button onClick={this.changeMyVariable(a, b, c,2)} type="button">Add more</button>
        );
        
    }
}
Wakeel

Firstly You need to use state to make your component render when type changes

constructor(props){
    super(props)
   this.state = {
     type: 1
  }
}

render(){
    let pickers  =  this.changeMyVariable(a, b,  c,  this.state.type);
   return (<>{pickers}
<button onClick={() => this.setState({type: 2})} /></>
);
}

Secondly You didn't handle button click event very well before. Your function would run immediately after render not on button click.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to change button value on click?

How to change the value of button on click?

Change variable value on button click in HTML

How to change change value of variable every time when I click on button

How to change image on button click with React Hooks?

React How to change view after click on button

How to change the state value using a click button

ReactJS: How to change Text value on button click

how to set value on button click in react js?

How to change the value of a variable on click in meteor

How to change value of a variable with a button press in Kivy

React Button Click should change the display value of div

How to change screen in react native with react-navigation on click of a button?

change button icon on click react

Change style on button click react

How can I change CSS variable on button click?

change cell value on button click

Change input value on Button Click

How to change color in react on button click using state

How to change style of a different component on button click in react?

How to change the "accept" value of input file on radio button click

How can i change my model value on button click in Razor?

How do I change a value in django on button click in HTML?

How to change the value inside a table when click a button?

How to change elements to default value after button click?

How to: click button, change html span (text) to whatever input value

Objective C - How to change the UIslider value with button Click actions

How to change the text field value from fragment to button click event

How to modify a redux variable state from react component with a button click?