All jobs

Replace connect With useSelector and useDispatch in a React Redux App

Yes. The agent rewrites each connected class as a function component with useSelector and useDispatch, pins behaviour with tests first, and fixes the cleanups.

Run this on RushOpens the console with this prompt ready to send, in a Project that has your repository.

What the agent does

Hooks only run in function components, so the job is larger than swapping one import. The agent first counts what is connected and how. In an app like bezkoder/react-redux-jwt-auth four files call connect: src/App.js, login.component.js, register.component.js and profile.component.js. All four are classes, all four pass only mapStateToProps, and none passes a second argument, so dispatch arrives as a prop and the code calls this.props.dispatch(...). The three board components and the home page are classes too but never touch the store, and the agent leaves them alone. package.json has react-redux ^7.2.1 on React 16.13.1, and the hooks API has been in react-redux since 7.1, so nothing needs upgrading.

There are no tests in the repository, so the agent writes them before it converts anything. @testing-library/react is already a dependency and react-scripts test runs Jest 24. The tests render each connected class inside a real Provider and a router, with src/services/auth.service.js mocked, and assert what a user sees: the profile page redirects to /login without a user, a rejected login shows the message from the store and re-enables the button, a successful registration hides the form. The same tests must pass unchanged after the rewrite. That is the proof the pull request carries.

The conversion then goes file by file, and the class lifecycles are where the decisions are. App.js registers history.listen in its constructor to clear the message on navigation and never unsubscribes; as a function it becomes a useEffect that returns the unlisten function history.listen hands back. App.js also copies this.props.user into component state once, in componentDidMount, and never again, which is why login.component.js calls window.location.reload() right after history.push("/profile"): without the reload the navbar would keep showing Login. With useSelector((state) => state.auth.user) the navbar is derived from the store on every render and the copy disappears. The agent keeps the reload, because removing it changes behaviour the issue did not ask about, and lists it as now redundant.

Two smaller traps get the same treatment. src/common/EventBus.js wraps every callback in an anonymous listener, and App.js calls EventBus.remove("logout") with no callback at all, so nothing was ever removed; an effect cleanup written the same way would be a known leak, so the agent makes on return a function that removes the listener it added. And mapStateToProps in the login component returns an object built from two slices. Written as one useSelector that returns a new object, it would re-render on every store update, since useSelector compares with strict equality. The agent writes one selector per value. The react-validation refs, this.form and this.checkBtn, become useRef objects, and the route's history prop is still passed to a function component, so navigation code stays as it is.

Start it with one API call

POST https://api.phoenix.so/o/{org}/sessions with this body:

{
  "agent": "codex",
  "input": "A Linear issue labelled hooks asks to replace react-redux connect with hooks in this React app. Read package.json and every file under src first. List each component that calls connect, whether it is a class, what its mapStateToProps reads, whether it passes mapDispatchToProps, and how it uses dispatch. Check that the installed react-redux and React versions support hooks and say so; do not upgrade either. Before converting anything, write tests with the testing library already in the project that render each connected component inside a real Provider and router with the HTTP service mocked, and assert user-visible behaviour including the failure paths; run them against the current code and show the output. Then convert only the connected components to function components using useSelector and useDispatch. Use one selector per value rather than a selector that returns a new object. Turn constructor and lifecycle side effects into effects with correct dependency arrays and real cleanups; where an existing subscribe helper cannot be unsubscribed, fix the helper minimally and say so. Keep refs to third-party form components working. Do not change routing, the store, reducers, thunks, markup or class names, and do not convert components that are not connected. If you find behaviour that only existed to work around the old wiring, leave it in place and list it. Run the same tests again, run the build with CI=true so lint warnings fail it, and open a pull request with the per-component table, both test outputs and the list of things left for a follow-up.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
  1. Get an API key. In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
  2. Send the request. POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
  3. Read what came back. Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

What comes back

The pull request has two commits. The first adds the test files and nothing else, and its message quotes the passing run against the class components. The second rewrites the four connected files and touches src/common/EventBus.js. Reading them in that order lets a reviewer confirm that the tests were not adjusted to fit the new code: the test files do not appear in the second commit.

The description holds a table with one row per component: what it selected through mapStateToProps, the useSelector calls that replace it, each lifecycle method and the effect it became, and the cleanup each effect returns. Below it is the build output with CI=true. That setting matters here because Create React App then treats ESLint warnings as errors, and its config includes the react-hooks/exhaustive-deps rule, so an effect with a missing dependency fails the build instead of scrolling past.

What a reviewer should check by hand is the short list at the end: the window.location.reload() that is kept but no longer needed, the EventBus.on change, and any effect whose dependency array the agent had to reason about, such as the one that subscribes to history. The store, the thunks and the reducers are not in the diff, and connect is gone from src entirely, which a search in the pull request's changed files confirms.

Run it on every Linear issue labelled hooks

Conversions like this are best fed one screen or one feature at a time. Add an automation to the Project in the console with a linear trigger on linear.issue.labeled and hooks in its labels. The saved prompt carries the method: inventory, tests first, convert only connected components, real cleanups, list what was left. The labelled issue is placed after the prompt as the event, and it is where a teammate names the components in scope.

A linear trigger needs a Linear connection for the member who last changed the automation, made in the console under Settings → Connections, and it fires only when the person who added the label is in your Rush org. Two issues that touch the same root component will produce pull requests that conflict there, so the automation's max_runs_per_hour is worth setting low for this job; an event past it waits queued until the hour has room.

The use case this job follows, with its automation and trigger: Turn a Linear label into a PR.

Why run it on Rush?

A hooks conversion is easy to get subtly wrong and hard to review by eye, because the bugs are in timing: a listener that is never removed, an effect that re-subscribes on every render, a value that used to be copied once and is now live. The agent's answer is tests that ran against the old code and the new code, in a sandbox where both runs happened on the same install, with the output of each kept in the session's transcript. The reviewer reads evidence, not a claim that it should behave the same.

It also parallelises in a way a person does not. A large app may have dozens of connected components owned by different teams. Labelled issues can start separate runs, each in its own sandbox with its own checkout, and each returns a pull request scoped to the components its issue named. Nobody's working tree is tied up while Jest runs.

Frequently asked questions

Is connect deprecated?

No. connect is still supported and existing code can stay on it. The React Redux documentation recommends hooks as the default for new code, and teams usually convert so that components read the same way everywhere. If part of the app should stay on connect, say so in the issue and the agent leaves those files out.

Do I need to upgrade react-redux or React first?

Not if you are on react-redux 7.1 or later with React 16.8 or later, since both hooks APIs exist from those versions. The agent reads the installed versions from the lockfile and states them. It does not upgrade either package as part of this job.

Can it keep my class components and still use hooks?

A class cannot call a hook. The options are to convert the class to a function, which is what this job does for connected components, or to leave it on connect. The agent does not add wrapper components whose only purpose is to pass hook results down as props.

What does it do with mapDispatchToProps?

In the fixture described here no component passes one, so dispatch was already a prop and becomes useDispatch(). Where a component does pass an object of action creators, the agent calls dispatch(creator(args)) at each use and keeps the creator imports, and notes any place where a bound creator was handed to a child as a callback.

More jobs like this: Turn a Linear label into a PR

Keep reading