Ordering issues when using View State

When the attached view is opened (via the static render function), it doesn’t load in the state until after onOpen has returned. I can tell this due to the console log debugging output.

Is this behavior expected or am I doing something wrong? This part of the API isn’t particularly well documented and the only thing I’ve been able to find about it is this FAQ.

export class ProjectView extends ItemView implements PersistedState {
	projectId: string;
	projectName: string;
	root: Root | null = null;

	constructor(leaf: WorkspaceLeaf) {
		super(leaf);
		this.navigation = true;
		this.projectId = "test";
		this.projectName = "test";
	}

	getViewType() {
		return VIEW_TYPE_PROJECT;
	}

	getDisplayText(): string {
		return "Project";
	}

	getIcon(): string {
		return "folder-kanban";
	}

	override async setState(
		state: PersistedState,
		result: ViewStateResult
	): Promise<void> {
		this.projectId = state.projectId;
		this.projectName = state.projectName;
		console.log("setState", state, result);
		return super.setState(state, result);
	}

	override getState(): PersistedState {
		return {
			projectId: this.projectId,
			projectName: this.projectName,
		};
	}

	static async reveal(currentLeaf: WorkspaceLeaf, project: Project) {
		await currentLeaf.setViewState({
			type: VIEW_TYPE_PROJECT,
			active: true,
			state: {
				projectId: project.id,
				projectName: project.name,
			} as PersistedState,
		});
	}

	async onOpen() {
		console.log("onOpen", this.projectId, this.projectName);
		this.root = createRoot(this.containerEl.children[1]);
		this.root.render(
			<StrictMode>
				<QueryClientProvider client={queryClient}>
					<AppContext.Provider value={this.app}>
						<ProjectComponent
							projectId={this.projectId}
							projectName={this.projectName}
						/>
					</AppContext.Provider>
				</QueryClientProvider>
			</StrictMode>
		);
	}

	async onClose() {
		this.root?.unmount();
	}
}

Worked out how to do this! You have to pass a function into the Component rather than the value.

	async onOpen() {
		this.root = createRoot(this.containerEl.children[1]);
		this.root.render(
			<StrictMode>
				<QueryClientProvider client={queryClient}>
					<AppContext.Provider value={this.app}>
						<ProjectComponent
							viewState={() => ({ projectId: this.projectId, projectName: this.projectName })}
						/>
					</AppContext.Provider>
				</QueryClientProvider>
			</StrictMode>
		);
	}