Search code examples
reactjsreact-nativetextinputreact-native-textinput

Activate and deactivate textInput


StackOverflow I'm trying to make a button to change the boolean value from true to false and vise versa in order to change editable value in textInput.

This is how far I got till this moment:

  const [change, setChange] = useState(true);
  const [text, setText] = useState("");

  const textEdit = () => {
    change === true ? setChange(false) : setChange(true);

  };

For some reason after hitting below TouchableOpacity, it won't change the state or there must be a problem in my code that prevent react from changing the state of "change" in the below text input

<TextInput
      style={{ width: "100%", height: 100, backgroundColor: "gray" }}
      placeholder={"write something"}
      onChangeText={(text) => setText(text)}
      editable={change}
    >
    <Text>adderess</Text>
</TextInput>

<TouchableOpacity
      onPress={() => {
        textEdit;
      }}
    >
      <Text style={{ color: "blue" }}>Change</Text>
</TouchableOpacity>

Solution

  • Because you don't call function textEdit on press. Just update like this:

    onPress={() => {
      textEdit();
    }}
    

    Or cleaner:

    onPress={textEdit}