Palantir Slate: state give different value in Code sandbox and handlebar

I working with code sandbox in Palantir Slate, but it seems to be kept in cache.

Let me explain: with code sandbox, I add/remove html element dynamically by clicking on buttons and check the state either in a text widget of the Slate with {{w_code_sandbox.state.myVariable}} and with console.log(SlateFunctions.getState().myVariable) after each deletion (myVariable is an object containing data from htlm element).

That works fine, except that when I remove the last element, console.log show me an empty object, but the text widget remains identical, with one element, as if the handlebar keep a cache. It’s very problematic since later, I’ll need the state for calculation outside the code sandbox.

Have you already met this problem? have you any solution? Thanks you for your help.

############################################################## Solution from ZettaP in comment: initiate the state variable with null and not empty object/string

Question originally asked by Turvy on Stack Overflow: codesandbox - Palantir Slate: state give different value in Code sandbox and handlebar - Stack Overflow

Can you share your (anonymized/non-sensitive) code for this ? What is exactly your variable composed of ? Is it an array or an object ? What are you deleting : fields of the objects or elements of an array ? So when you delete the last element, what do you get : an empty array, an empty object, no array or no object ?

ZettaP

Aug 9, 2023 at 6:48

  • Hi @ZettaP, in this case, my variable is an object, but I encounter a similar problem with a string in another project. When I reinit the variable (by deleting all its key or create an empty object/string), a log in js of code sandbox show me the good variable content (i.e nothing), but not handlebar in Slate which always show me the last key/string, as if it keep cache. However, if I had a new key/string, everything is good, only the last element is visible.

Turvy

Aug 9, 2023 at 8:23

  • 1

Is your initial array empty ? Are you setting the value of your variable to what it is originally in the template ? What happens if you were to change the initial value ? (like instead of starting from or {}, start from undefined)

ZettaP

Aug 9, 2023 at 11:01

  • 1

Hurray!!! that works now. The initial variable was initiated with an empty object, but I’m just trying with null instead (undefined was not accepted) and it works fine. When deleting the last key, handlebar show me an emty object

Turvy

Aug 9, 2023 at 11:45

  • Added as a real answer, as it seems that’s a viable workaround :slight_smile: Glad that it worked !

ZettaP

Aug 10, 2023 at 6:25

Add a comment

2 Answers

Sorted by:

2

As a workaround, you can initialize your template with a value different than what is the “empty state” you might encounter during the standard operation of the app.

For instance, if you have an array, which you push and delete elements from, initialize it by an empty value (null, for instance) instead of empty array ([]).

For an object, initialize it as well by undefined, null, instead of empty object ({}).

Share

Improve this answer

Follow

answered Aug 10, 2023 at 6:25


ZettaP's user avatar

ZettaP

84988 silver badges1212 bronze badges

Add a comment

0

When deleting the element, I would suggest explicitly setting the variable state to an empty object (or whatever you want it to be) using the SlateFunctions.setstate() method (docs).

Share

Improve this answer

Follow

answered Aug 3, 2023 at 17:12


Ontologize's user avatar

Ontologize

62911 silver badge77 bronze badges

  • I’ve already done that, mais with the same result. Strangely, when I add a new element again, the state variable is good

Turvy

Aug 4, 2023 at 7:53

Answer originally provided by Ontologize on Stack Overflow: codesandbox - Palantir Slate: state give different value in Code sandbox and handlebar - Stack Overflow