using form_state->set() inside ajax callback but cannot get the value back outside the callback I want to add some arbitrary variable to store it in the form_state and use it again so in one of my ajax callback functions I set a property named var:

public function MyFunctionCallback(array &amp;$form, FormStateInterface &amp;$form_state) {
....
$terms = array('1'=&gt;'one','2'=&gt;'two');
$form_state-&gt;set('var', $terms);
....
}


it works and the storage array of the form_state has var element

unfortunately when trying to get the var back inside another callback function it returns null.
actually with debugging the storage array of the form_state is empty

public function MyOtherFunctionCallback(array &amp;$form, FormStateInterface &amp;$form_state) {
....
$terms = $form_state-&gt;get('var');
....
}
As 4k4 mentioned, you cannot change the form state inside an ajax callback function.

You can, however, change it in the ajax button submit function.

$form['ajax_button']['#submit'][] = [static::class, 'ajaxButtonSubmit'];

public static function ajaxButtonSubmit($form, $form_state) {
  //...
}