Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
169
rated 0 times [  176] [ 7]  / answers: 1 / hits: 15251  / 6 Years ago, sat, may 5, 2018, 12:00:00

I wrote a very simple reactJs Code. I set fontSize as my state. I want to change my fontSize while I change the input value. But it does not work. Could anyone help? Thanks in advance.



Here is my js code:



import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';

class Test extends React.Component{
constructor(props){
super(props)
this.state={
fontSize:64
}
}
changeSize(event){
this.setState({
fontSize:event.target.value
});
}

render(){
let styleobj = {background:blue,fontSize:64}
return(
<section style={styleobj}>
<h2 className=tryout style={{fontSize:this.state.fontSize}}>{this.state.fontSize}</h2>
<input value={this.state.fontSize} onChange={this.changeSize.bind(this)}/>
</section>
);

}
}


ReactDOM.render(<Test name=Sumei value=123/>,document.getElementById(root));

More From » reactjs

 Answers
0

You need to specify the value of font size (em, px, rem, % ...)



  render() {
let styleobj = { background: blue, fontSize: 64 }
return (
<section style={styleobj}>
<h2 className=tryout style={{fontSize: this.state.fontSize+'px'}}>{this.state.fontSize}</h2>
<input value={this.state.fontSize} onChange={this.changeSize.bind(this)} />
</section>
);

}
}


Edit


[#54503] Wednesday, May 2, 2018, 6 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
isham

Total Points: 69
Total Questions: 86
Total Answers: 86

Location: Anguilla
Member since Sun, Jan 29, 2023
1 Year ago
;