Hi, I'm venturing into React Js and I'm looking at how props and state behave. I'm passing some props through the ReactDom and I try to manage them and I see that they do not change their initial state. Now I'm working with the states and I see if they change. Well first I want to clarify how this actually works in React. My second question is because in my code I am trying to increase the value of a state and only the subtraction works correctly when the two functions are declared in the same way? Below I put the example code so that they look at the behavior of the function _handleIncreaseButton ().
<style>
h2{
border-top-right-radius: 5px;
border-top-left-radius: 5px;
color: #fff;
background-color: #0288D1;
margin-bottom: 0px;
padding: 10px;
}
button{
margin: 0 10px 0 10px;
}
#text-style,#text-size {
display: block;
margin: 10px 0 10px 0;
}
#list{
border: 1px solid #0288D1;
background-color: #fff;
margin-top: 0px;
padding: 10px
}
#menu{
display:inline-block;
}
</style>
<script src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/[email protected]/babel.min.js"></script>
<script src="FontChooser.js" type="text/jsx"></script>
<div id='container'></div>
<script type="text/jsx">
ReactDOM.render(
<div>
<FontChooser min='4' max='40' size='16' text='Fun with React !' bold='false'/>
</div>,
document.getElementById('container')
);
</script>
/ ------ React Code ------ /
class FontChooser extends React.Component {
constructor(props) {
super(props);
this.state = {
size: this.props.size,
textStyle: this.props.Bold,
min: this.props.min,
max: this.props.max
};
}
_handleDecreaseButton(){
this.setState({size: this.state.size - 1});
}
_handleIncreaseButton(){
this.setState({size: this.state.size + 1});
}
render() {
return(
<div id="bloque">
<div id="menu" >
<h2 >Text Edit</h2>
<div id="list" >
<div id="text-style">
<label id="checkbox-label">Transform to Bold: </label>
<input type="checkbox" id="boldCheckbox" />
</div>
<div id="text-size">
<label id="checkbox-label-size">Font Size: </label>
<button id="decreaseButton" onClick={ this._handleDecreaseButton.bind(this)}>-</button>
<span id="fontSizeSpan" >{this.props.size}</span>
<button id="increaseButton" onClick={ this._handleIncreaseButton.bind(this)}>+</button>
</div>
</div>
</div>
<div>
<span id="textSpan">{this.props.text}</span>
<span id="textSpan">{this.state.size}</span>
</div>
</div>
);
}
}