Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making font weight bold by passing value in React.js

Tags:

reactjs

I want to change a font weight from normal to bold by clicking checkbox input.

I tried to apply a ternary operator but it didn't work.

bold={ this.state.checkboxState ? this.props.bold : !this.props.bold }

What is the best way to implement this functionality?

Here is a codepen demo.

The code

class FontChooser extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            hidden: true,
            checkboxState : true
        }
    }
    toggle(e) {
        this.setState({
            checkboxState : !this.state.checkboxState
        })
        console.log(!this.state.isChecked)
    }

    render() {
        return(
            <div>
                <input type="checkbox" 
                    id="boldCheckbox"
                    checked={this.state.isChecked}
                    onClick={this.toggle.bind(this)}
                />
               <span 
                   id="textSpan" 
                   bold={ this.state.checkboxState ? this.props.bold : !this.props.bold }
               >
                   {this.props.text}
               </span>
           </div>
       );
    }
}

React.render(
    <div>
        <FontChooser text='Fun with React!' bold='false' />
    </div>,
    document.getElementById('container')
);

Thank you for your kind advice.

like image 991
aaayumi Avatar asked Oct 06 '17 21:10

aaayumi


People also ask

How do I change the font weight in React JS?

Use inline styles to bold specific text in React. js, e.g. <span style={{fontWeight: 'bold'}}>world</span> . The bold font will only be applied to the element to which it was added and its children. Copied!

What is the font weight value for bold?

Note that when using relative weights, only four font weights are considered — thin (100), normal (400), bold (700), and heavy (900).

How do you style fonts in React?

✨ Using the Font link Go to https://fonts.google.com/. Here we are using the same font-family that linked in the above step. Last, you can add this style anywhere in your React component. const FontLink = () => { return( <div className="card"> <span className="font-link"> This is with Font Link.

How do you Unbold text in React JS?

You can apply the font-weight: normal as mentioned using the data attribute used by react-tooltip. Save this answer.


1 Answers

Change <span id="textSpan"> to the following:

<span 
    id="textSpan" 
    style={ this.state.checkboxState ? { fontWeight: 'normal' } : { fontWeight: 'bold' } }
>
    { this.props.text }
</span>
like image 57
wlh Avatar answered Oct 21 '22 09:10

wlh