Structural assumptions: $X$ is a nontrivial connected separable topological space
Given: $f(x)=g(h(x))$.
$f:X\to\mathbb R$ is continuous
$h:X\to\mathbb R$ is continuous and $h(X)=\mathbb R$.
$g:\mathbb R\to\mathbb R$.
Question: Can we say $g$ is also continuous?
Motivation: Composition of continuous functions is also continuous. Does an "inverse" argument holds?
After doing some research the argument seems to be correct for metric spaces with additional assumptions of one of the function being homeomorphism: If the composition of two functions is continuous and one of those functions is continuous, is the other function continuous as well?
Note: superscripts in this proof are indexes not exponents.
My test: I think if we let $X$ be path-connected, it is easy to prove that $g$ is continuous. Let $y_1<y_2$ be two real numbers and $h(x_i)=y_i$, for any real number $y\in (y_1,y_2)$, consider a sequence $y^n\in (y_1,y_2)$ converging to $y$. We will show that $g$ is continuous at arbitrary real number $y$.
Let $T$ be the path between $x_1,x_2$. It follows from the continuity of $h$ that for each $n$ there exists $x^n\in T$ such that $h(x^n)=y$. Because of path-connectedness, path $T$ is compact. So sequence $x^n$ converges to $x$ where $f(x)=y$. By the continuity of $f$, it follows that $f(x^n)$ converges to $f(x)$ and $g(h(x^n))$ converges to $g(h(x))$. Finally, we conclude that $h(x^n)\to h(x)$ $\implies g(h(x^n))\to g(h(x))$.
However, I think this method only works for path-connected $X$ not connected $X$. Could you please give me some hints or a counterexample?
[Updated with significantly simplified counterexample.]
No, we cannot say $g$ is continuous for the general connected case. We have a counterexample already with $X\subseteq \mathbb R^2$.
Let $Y^0\subseteq \mathbb R^2$ be the $y$-axis with the origin removed, and for each $n\in\mathbb Z\backslash \{0\}$ let $J_n=(0,1)\times\{\frac{1}{n}\}$. Denote also $J_\infty= (0,1)\times\{0\}$.
Let $$X=Y^0\cup J_\infty\cup \bigcup_{n\neq 0} J_n,$$ and note that $X$ is connected but not path connected.
Let $h\colon X\to\mathbb R$ be the $y$-coordinate function restricted to $X$, (which is clearly continuous and surjective) and define $g\colon \mathbb R\to \mathbb R$ by $$g(t)= \begin{cases} \sin(\frac{\pi}{t}) &t\neq 0\\ 0 & t=0 \end{cases} $$
so that $g$ is discontinuous at $0$ but continuous everywhere else.
Now $f=g\circ h$ is certainly continuous on each $J_n$, including $J_\infty$. To see this, note that the composition is identically equal to $0$ on the union of these sets, which is open in $X$ (in fact it is given by $X\cap\{(x,y)\in\mathbb R^2\mid x>0\}$). Moreover, $f$ is continuous at each point in $Y^0$, since $h$ is nonzero there, and $g$ is continuous away from $0$. Hence $f$ is continuous at every point.