Home > Mobile >  I am trying to change fontweight when click on button
I am trying to change fontweight when click on button

Time:08-12

I am trying to change fontWeight of text when click on button

`

const [fontWeightt, changefontWeight] = useState('bold')
<TouchableOpacity
onPress={changefontWeight('normal')}>
<Text style={{fontWeight:`${fontWeightt}`}}>Hello</Text>
</TouchableOpacity>

`

but this error appears: No overload matches this call.

CodePudding user response:

Instead of this:

onPress={changefontWeight('normal')}>

Do this

onPress={() => changefontWeight('normal')}>

CodePudding user response:

Try this one.

On click of text you can change Font-weight, same way you can apply in button.

import React from 'react';
import {TouchableOpacity, Text} from 'react-native';
import {useState} from 'react';

export function TextBoldChange() {
  const [count, setCount] = useState(0);

  return (
    <TouchableOpacity onPress={() => setCount(!count)}>
      {count ? (
        <Text style={{fontWeight: 'normal'}}> Hello </Text>
      ) : (
        <Text style={{fontWeight: 'bold'}}> Hello </Text>
      )}
    </TouchableOpacity>
  );
}

CodePudding user response:

If you want to switch between two weights, it could be achieved easily with the boolean state. Example:

const [fontWeightt, changefontWeight] = useState(false)

<TouchableOpacity
   onPress={()=> changefontWeight(!fontWeightt)}
>
  <Text style={{fontWeight:fontWeightt ? "bold" : "normal"}}>Hello</Text>
</TouchableOpacity>
  • Related