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

Is there a way to manually refresh the panel and values? #26

Open
szemyd opened this issue Nov 5, 2020 · 4 comments
Open

Is there a way to manually refresh the panel and values? #26

szemyd opened this issue Nov 5, 2020 · 4 comments

Comments

@szemyd
Copy link

szemyd commented Nov 5, 2020

I'd like to refresh values according to a change in an api result: I have now a few disabled inputs and if the user is authenticated I'd like to enable those.

Is there a way to do this?

Thanks in advance!

@birkir
Copy link
Owner

birkir commented Nov 20, 2020

Give it your own state.

function SomeComponent() {
  const state = useState(0);
  const ctrl = useControl('foo', { state })
  const [value, setValue] = state;
  const onApiUpdate = (newValue) => {
    setValue(newValue);
  };
}

Or if you have the ID, you can get the "setter".

import { ControlsContext } from 'react-three-gui';

function SomeComponent() {
  const context = useContext(ControlsContext);
  const ctrl = useControl('foo', { id: 'c1' })
  const onApiUpdate = (newValue) => {
    const setValue = context.state.current.get('c1');
    setValue(newValue);
  };
}

@borg
Copy link

borg commented Nov 23, 2020

Testing this and found that using the id as set in the config would still return a null using the context.state.current.get('myId'). Tracing out the state seems to indicate the id is not actually stored in the state.

@JacobJaffe
Copy link

JacobJaffe commented Dec 4, 2020

I'm unable to get this working with a basic use case.
using state does trigger an onChange for the new value, but does not seem to change the input itself.

const Comp = () => {
  const state = useState(0);
  const [value, set] = state;

  useControl("Foo", {
    type: "number",
    value: value,
    state: state,
    min: -5,
    max: 5,
    scrub: false,
    onChange: (v) => console.log(v), // This DOES get called with the new value
  });

  useEffect(() => {
    setTimeout(() => {
      set(1); // This does not update the displayed value; the input remains at the 0 position
    }, 1000);
  }, []);

  return null;
};

ffdead added a commit to 14islands/react-three-gui that referenced this issue Jan 22, 2021
@lobis
Copy link

lobis commented Apr 7, 2021

Hello @birkir, @ffdead,

Could you please provide a more detailed workaround for this issue? I have also tried @JacobJaffe and it indeed doesn't work.

In my particular use case I have a component that loads a 3D model into a state. My custom GUI component is a table of buttons each one pointing to one of the meshes of the model, since I have to load the model the state starts as undefined and I don't have access to the meshes.

I have tried many ways to update the GUI component after it has been loaded but the only way I could manage to do it is via document.querySelector(#id).appendChild which doesn't seem right.

Thanks.

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

5 participants