Attach a variable/object on the state?

Is there a way to attach an object on the EditorState instance? We created multiple states for a single editor view and need to store some variable values on each state accordingly. Is that possible?

I tried to following way but it always got overwritten.

let newState = EditorState.create({
    doc: initialDoc,
    extensions: [
        basicSetup
    ]
});
newState.myObject = myObject;

Look into state fields.

@marijn
Thanks for quick response! I tried stateFields but it works like computed property. my problem is that I want to store some variables for each state. See code below, if i do this, how can i find the statefield for each state?
What i want to implement is to access some unique information for each state. But i couldn’t find a way to store it along with each state. can this be done via statefield?


    let treeNodeField = StateField.define({
        create () { return myVariableFromOutside; }
    })

    let newState = EditorState.create({
        doc: initialDoc,
        extensions: [basicSetup],
        treeNodeField
    });

    newState.field(treeNodeField)

If the value stays the same as the state gets updated through transactions, a facet might be more convenient. If it doesn’t, you can use StateField.init to give a field a custom initial value.

Thanks! Could you tell details how to use facet to store value on the state?