Skip to content

Instantly share code, notes, and snippets.

@renanmav
Created August 14, 2019 23:57
Show Gist options
  • Save renanmav/d91edc3c7e08e0eafc62d288edd23cd5 to your computer and use it in GitHub Desktop.
Save renanmav/d91edc3c7e08e0eafc62d288edd23cd5 to your computer and use it in GitHub Desktop.
Closure to pass navigation instance to onCompleted function
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>
);
};
@renanmav
Copy link
Author

// TODO: implement optimistic to update the Relay store
    const onCompleted = (response: TweetCreateMutationResponse) => {
      if (!response.TweetCreate) return;

      const { content: success, error } = response.TweetCreate;

      error && Alert.alert(error);

      success && setContent("") && navigation.goBack();
    };

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment