Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document method to call on refs #419

Open
cem2ran opened this issue Apr 20, 2019 · 3 comments
Open

Document method to call on refs #419

cem2ran opened this issue Apr 20, 2019 · 3 comments

Comments

@cem2ran
Copy link
Contributor

cem2ran commented Apr 20, 2019

Bringing this over from Discord as requested by @MoOx. I'm basically not sure if this is the right way to use refs and if the external I've defined in the following example should be exposed in the repo:

open ReactNative;
open ReactNavigation;

// TODO: add to TextInput?
external elementToObj: TextInput.element => Js.t({..}) = "%identity";

[@react.component]
let make = (~navigation: Navigation.t) => {
  open Navigation;
  let (text, onChangeText) = React.useState(() => "Home");
  let ref = React.useRef(Js.Nullable.null);

  React.useEffect0(() => {
    let intervalId =
      Js.Global.setTimeout(
        () =>
          switch (ref->React.Ref.current->Js.Nullable.toOption) {
          | Some(_ref) => _ref->elementToObj##focus()
          | None => ()
          },
        5000,
      );
    Some(() => Js.Global.clearTimeout(intervalId));
  });

  <>
    <TouchableOpacity
      onPress={_ => navigation->Navigation.navigate("PrivacyPolicy")}>
      <Text> {React.string(text)} </Text>
    </TouchableOpacity>
    <TextInput ref onChangeText={text => onChangeText(_ => text)} />
  </>;
};
@cem2ran
Copy link
Contributor Author

cem2ran commented Apr 20, 2019

@cem2ran cem2ran closed this as completed Apr 20, 2019
@renanmav
Copy link

So what would be the correct solution here?

@MoOx
Copy link
Member

MoOx commented Jun 9, 2020

We should definitely document most common method to use via ref in the doc.

@cem2ran can you confirm that focus can be done via something like

        textInputRef
        ->React.Ref.current
        ->Js.Nullable.toOption
        ->Option.map(textInput =>
            textInput->TextInput.focus()
          )
        ->ignore,

@MoOx MoOx reopened this Jun 9, 2020
@MoOx MoOx changed the title Refs in reason-react-native Document method to call on refs Jun 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants