How are state taxes determined if you lived in 3 states during one year?

Here is a scenario:

- We live in California for 6 months (up to June).

- Then we live in NJ for 3 months. (at someone's place)

- Finally, we move permanently to Pennsylvania.

I worked remotely all that time.

CA State taxes being the highest, how can I limit those? Should I become a NJ resident as soon as we are in NJ so that I stop paying CA state taxes?