0

I have a very long string that is over 1000 characters. I was just wondering if I could store that in the state without any problems. Also, I want to add this long string with a fairly short string. Will that be possible.

FYI: I am using useState from React hooks. Also, I am in react native, but I don't know if it changes anything.

EDIT: I am writing this question because when I store it in the state, then print it out, it console.log as undefined.

Here is my code:

const exportReport = async () => {
    if (validate()) {
      setLoading(true);
      let token = await SecureStore.getItemAsync("token");
      return await fetch("https://notarealapi.herokuapp.com/api/export", {
        method: "POST",
        headers: {
          "content-type": "application/json",
          Authorization: `JWT ${JSON.parse(token)}`,
        },
        body: JSON.stringify({ note: note, mission: value }),
      })
        .then((res) => res.json())
        .then(async (json) => {
          if (json.hasOwnProperty("msg")) {
            Alert.alert(json.msg);
          } else {
            setLoading(false);
            console.log(json);
            setReportUrl("data:application/pdf;base64," + String(json.pdf));
            console.log(reportUrl);
            const base64Code = reportUrl.split(
              "data:application/pdf;base64,"
            )[1];
            const filename = FileSystem.documentDirectory + "report.pdf";
            console.log(base64Code);
            await FileSystem.writeAsStringAsync(filename, base64Code, {
              encoding: FileSystem.EncodingType.Base64,
            });
            const _mediaResult = await MediaLibrary.saveToLibraryAsync(
              filename
            );
            await Sharing.shareAsync(filename);
          }
        })
        .catch((error) => console.error(error));
    }
  };

Any help will be appreciated.

Thank you

fwara07
  • 45
  • 1
  • 8
  • It shouldn't be an issue to store any long string. But you can always test using console.log(sth) to see if the string is being stored properly – Punreach Rany Jul 02 '21 at 01:00
  • Sure. Did you try it yourself? – Drew Reese Jul 02 '21 at 01:00
  • It just prints the string but truncates to the first 1000 characters. – fwara07 Jul 02 '21 at 01:01
  • 1
    React state updates are asynchronous, you can't immediately log the state right after enqueueing an update. See https://stackoverflow.com/questions/54069253/usestate-set-method-not-reflecting-change-immediately – Drew Reese Jul 02 '21 at 01:06
  • Does that mean I have to create a `sleep` function after setting the state? – fwara07 Jul 02 '21 at 01:07
  • 1
    No. Use the component lifecycle to your advantage. Log the updated state in `componentDidUpdate` or `useEffect` with appropriate dependnency. – Drew Reese Jul 02 '21 at 01:08
  • you don't need a sleep function. if you put the console.log in the body of your component function it will log twice, once before the state is set and once after.. assuming you're not hardcoding the state – azium Jul 02 '21 at 01:08
  • 1
    That's not a big string. Your problem is surely in your code which you have not shared. Please share the code and I bet we can fix it. – windowsill Jul 02 '21 at 01:09
  • I added my code. – fwara07 Jul 02 '21 at 01:11
  • How would I use `useEffect`? – fwara07 Jul 02 '21 at 01:14
  • 1
    I'm guessing it is the `reportUrl` state you are trying to log, use `useEffect(() => console.log(reportUrl), [reportUrl]);` to log state updates. – Drew Reese Jul 02 '21 at 01:33

0 Answers0