0

I'm doing a project in react native (typescript) and here is my issue : I have this error Argument of type 'GestureResponderEvent' is not assignable to parameter of type 'SetStateAction<string>'.ts(2345) with this part of my code, at the i of setSelected :

<View style={{}}>
          <Text style={styles.texteti}>Heading, paragraphe, lien</Text>
          {state.map((i) => (
            <TouchableOpacity onPress={(i) => setSelected(i)}> 
              <Text>{i} Here</Text>
            </TouchableOpacity>
          ))}
          {selected == "v1Visible" && (
            <View>
              <Text>View 1</Text>
            </View>
          )}
          {selected == "v2Visible" && (
            <View>
              <Text>View 2</Text>
            </View>
          )}
          {selected == "v3Visible" && (
            <View>
              <Text>View 3</Text>
            </View>
          )}
          {selected == "v4Visible" && (
            <View>
              <Text>View 4</Text>
            </View>
          )}
        </View>

Anyone has an idea of how to resolve this

1 Answer 1

1

You are reusing the variable with name i in the callback function passed to onPress of the TouchableOpacity. You are using the same variable name in your map function of state. Since the setter setSelected is called in the local scope of the callback function of onPress, the event passed to this callback function will be used (it is stored in i of the local scope of the callback function).

The easiest fix would be to either rename the variable or not declare it at all since you are not using it anyway.

{state.map((i) => (
       <TouchableOpacity onPress={() => setSelected(i)}> 
          <Text>{i} Here</Text>
       </TouchableOpacity>
))}

If you actually want to store the event of the onPress action (which seems to be unlikely judging from your current code), you need to either change the typing of your state or access whatever you want to access from the event.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.