How to use a mutable value in WebViewer?

WebViewer Version: 8.11.0

Do you have an issue with a specific file(s)? No.
Can you reproduce using one of our samples or online demos? Yes.
Are you using the WebViewer server? No.
Does the issue only happen on certain browsers? No.
Is your issue related to a front-end framework? Yes.
Is your issue related to annotations? No.

Please give a brief summary of your issue:
(Think of this as an email subject)

How to use a mutable value in WebViewer?

Please describe your issue and provide steps to reproduce it:
(The more descriptive your answer, the faster we are able to help you)

I want to use mutable values in webViewer like useState, but webViewer created once and can’t find latest value.

Dev spec : NextJs(13.3.1), React(18.2.0), Typescript(4.9.3)

Please provide a link to a minimal sample where the issue is reproducible:

	const [prevDisplayMode, setPrevDisplayMode] = useState('Continuous')

	useEffect(() => {
		console.log('::: useEffect : prevDisplayMode ', prevDisplayMode)
	}, [prevDisplayMode])

	useEffect(() => {
		if (forwardRef.current === null) {
			WebViewer(
				{
					licenseKey: MYKEY,
					path: '/express-plus/lib',
					...pdftronSettings,
				},
				viewer.current,
			)
				.then((instance: WebViewerInstance) => {

				...

				instance.UI.addEventListener('fullscreenModeToggled', (e, b) => {
					console.log('prevDisplayMode ', prevDisplayMode)
				})

				documentViewer.addEventListener('displayModeUpdated', (e) => {
					const currentDisplayMode = documentViewer
						.getDisplayModeManager()
						.getDisplayMode().mode

					setPrevDisplayMode(currentDisplayMode)
				})

				...


				})
				.catch((error) => {
					console.log(error)
				})
		}
	}, [])


1 Like

Hi moldy

Can you please clarify, what do you mean by “WebViewer created once and can’t find latest value.”

1 Like

I mean,
After webviewer is created once, it doesn’t find the latest value.

1 Like

Hi moldy,

We have tried

documentViewer.addEventListener('displayModeUpdated', (e) => {
					const currentDisplayMode = documentViewer
						.getDisplayModeManager()
						.getDisplayMode().mode

					setPrevDisplayMode(currentDisplayMode)
          console.log("Test",currentDisplayMode)
				})

Console log here, returned the correct value every time. Can you please provide us with more clarification on what are you trying to achieve? Video of you reproducing the issue would help.

1 Like

The latest value should be the prevDisplayMode(the value in useState) in fullscreenModeToggled, not the currentDisplayMode.

1 Like

Did switching this work for you?
Can you provide the code snippet youre using?

Best reagrds,
Tyler

1 Like