Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ReactJs: fontSize does not change when calling it from a font change method

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"));
like image 957
Sue-May Xu Avatar asked May 05 '18 16:05

Sue-May Xu


1 Answers

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 w2p3kwmv15

like image 64
Liam Avatar answered Nov 14 '22 23:11

Liam