Double H
Double H

Reputation: 27

TouchableOpacity's onPress is not working

Task.js

export const Task = ({ addTask }) => {
  const [focusItem, setFocusItem] = useState(null);
  return (
    <View style={styles.titleContainer}>
      <Text style={styles.title}>What would you like to focus on?</Text>
      <View style={styles.container}>
        <TextInput
          style={{ flex: 1 }}
          maxLength={50}
          value={focusItem}
          onSubmitEditing={()=>({ nativeEvent: { text } }) => setFocusItem(text)}
        />
        <RoundedButton
          style={styles.addSubject}
          size={50}
          title="+"
          onPress={()=>addTask(focusItem)}
        />
      </View>
    </View>
  );
};

App.js

import React, { useState } from 'react';
import { Text, View, StyleSheet } from 'react-native';
import Constants from 'expo-constants';
import {Task} from './src/features/task/Task'

export default function App() {
  const [task, setTask] = useState(null);
  return (
    <View style={styles.container}>
     
        {
          task ? 
          (<Text></Text>):
          (<Task addTask = {setTask}/>)
        }
        <Text>{task}</Text>
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#425670',
  },
});

I have tried to send the data from Task to App component by setting the value from Task. But onPress is not working. I could see the text has been set successfully while executing the onSubmitEditing, but the onPress is doing nothing. Please help me fix this. Thanks in advance

Upvotes: 0

Views: 245

Answers (1)

FnH
FnH

Reputation: 757

You need to change this

onSubmitEditing={()=>({ nativeEvent: { text } }) => setFocusItem(text)}

to

onSubmitEditing={({ nativeEvent: { text } }) => setFocusItem(text)}

You could also refer I want to update the parent according to the change of the child to react native

Upvotes: 1

Related Questions