/    Sign up×
Community /Pin to ProfileBookmark

If we convert these hooks to classes does the code still works?

Hi,

If we convert these hooks to classes does this code still works in react?

`import { useState } from “react”;
//the change is reflected in the ImageEditor component
const ImageEditor = ({ yourState }) => (
<p>State in the ImageEditor = {yourState}</p>
);
//ImageTile changes the state through the setYourState method
const ImageTile = ({ yourState, setYourState }) => (
<button onClick={() => setYourState(“World!”)}>
Change State from the ImageTile
</button>
);
//App is the parent component and contains both image editor and tile
const App = () => {
//the state which holds the image ID is declared in the parent
const [imageId, setImageId] = useState(“Hello”);
return (
<div>
<ImageTile yourState={imageId} setYourState={setImageId} />
<ImageEditor yourState={imageId} />
</div>
);
};
export default App;`

You can see the complete code on:

https://codesandbox.io/s/billowing-brook-9y9y5?file=/src/App.js:0-775

thank,

Saeed

to post a comment

3 Comments(s)

Copy linkTweet thisAlerts:
@bwclovisMay 04.2021 — My question is, why do you want to ? With React and its ecosystem moving towards more function with hooks components, what are you looking to accomplish with this re factor? Other than that, yes you can always refactor a React FC to a Class and vice versa. But again we have a rule set in place for "react/prefer-stateless-function" so what's the benefit you hope to achieve?
Copy linkTweet thisAlerts:
@lingo1357authorMay 04.2021 — I'm using an older template for react/ASP.netCore which consists of some class-based components?
Copy linkTweet thisAlerts:
@bwclovisMay 05.2021 — @lingo1357#1631214

I guess Im now wondering why your backend stack is so coupled with your front end one, either way its doable.
×

Success!

Help @lingo1357 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.27,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...