Created
August 14, 2019 23:57
-
-
Save renanmav/d91edc3c7e08e0eafc62d288edd23cd5 to your computer and use it in GitHub Desktop.
Closure to pass navigation instance to onCompleted function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const NewTweet: NavigationScreenComponent = ({ navigation }) => { | |
const [content, setContent] = useState(""); | |
const handleCreateTweet = () => { | |
const input = { | |
content | |
}; | |
// TODO: implement optimistic to update the Relay store | |
const onCompleted = (() => { | |
const nav = navigation; | |
return function(response: TweetCreateMutationResponse) { | |
if (!response.TweetCreate) return; | |
const { content: success, error } = response.TweetCreate; | |
error && Alert.alert(error); | |
success && setContent("") && nav.goBack(); | |
}; | |
})(); | |
const onError = () => { | |
Alert.alert("Algo deu errado ao criar o tweet"); | |
}; | |
TweetCreateMutation.commit(input, onCompleted, onError); | |
}; | |
return ( | |
<View> | |
<Buttons> | |
<CancelarButton /> | |
<TweetarButton disabled={!content.length} onPress={handleCreateTweet} /> | |
</Buttons> | |
<TweetInput | |
value={content} | |
onChangeText={t => setContent(t)} | |
placeholder="O que está acontecendo?" | |
autoFocus | |
multiline | |
/> | |
</View> | |
); | |
}; |
Author
renanmav
commented
Aug 15, 2019
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment