0

In this code when you click on button a screenshot should be taken and screenshot should be shown by updating useState, but i get this error: json.stringify cannot serialize cyclic structures how can i solve this?

let [ImageURI, setImageURI,]=useState(<Image source={{uri:'http://website.com/wp-content/uploads/image.jpg'}}/>);

Here the function is that we use for taking screenshot and updating useState:


const captureScreenFunction=()=>{

    captureScreen({
        format: "jpg",
        quality: 0.8
      }).then(
        uri => setImageURI(ImageURI=uri),
        error => console.error("Oops, snapshot failed", error)
      );

}

And here what is returns:


    return (


 <Fragment>

<Button
title='capture screen'
onPress={captureScreenFunction}

>
</Button>

<Image
source={{uri:ImageURI}}
style={{width:300, height:200, resizeMode:'contain', marginTop:15}}
></Image>

</Fragment>


    )

1 Answer 1

1

Well of course it is cyclic, because you use Image in the definition of Image itself:

<Image
  source={{uri:ImageURI}}
  style={{width:300, height:200, resizeMode:'contain', marginTop:15}}
></Image>

ImageURI, here, I assume, refers to ImageURI constructed in the hook definition:

let [ImageURI, setImageURI,]=useState(<Image source={{uri:'http://website.com/wp-content/uploads/image.jpg'}}/>);

I think the fix is easy, and also better, because state variables should be as simple as possible:

let [ImageURI, setImageURI,] = useState('http://website.com/wp-content/uploads/image.jpg');

That's also consistent with the data type you are setting it to later in your code (a string).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.