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

src property must be a valid json object while waiting for Promise to resolve #433

Open
Claim0013 opened this issue Nov 30, 2022 · 1 comment

Comments

@Claim0013
Copy link

Hey guys!

I found this issue but it didn't help at all -> #181

image

I am passing a fetched json to the src property and apparently the viewer isn't waiting for the promise to resolve and throws the exception instantly.

export const fetchJsonByURL = (url) => {
  return axios.get(url)
    .then((res) => res.data)
    .catch((error) => {
      toast.error(`${error.message} at getting ${url}`);
      return null;
   });
};

The promise resolves right after the error. If I directly pass a json object to src, it works. I use an async/await method and pass it down to the component as such:
<ReactJson src={getSrc()} collapseStringsAfterLength={40} ... />

Any tips?

@nathanwaters
Copy link

This works for me. Just adapt to suit axios...

  const [loading, setLoading] = useState(true);
  const [JSON, setJSON] = useState([]);

  useEffect(() => {
      fetch(url).then(async (x) => {
        const data = await x.json()
        setJSON(data)
        setLoading(false);
      });
  }, []);

  return loading ? <div /> : <ReactJson src={JSON} />;

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

2 participants