palantir-foundrycodesandbox

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


Solution

  • 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 ({}).