Refactoring from Hooks to a class componentHow to change an element's class with JavaScript?How do I remove a property from a JavaScript object?How do I remove a particular element from an array in JavaScript?How do I return the response from an asynchronous call?Refactor a React component from function to ES6 classReact functional stateless component, PureComponent, Component; what are the differences and when should we use what?How to turn this Component Class into a pure function Stateless Component (Typescript + React)Converting my class-based component into functional components using hooksHow does useState() in react retrieve the correct state object and function for a functional component when using the state hook?How to use React Hooks to refactor existing code?

Looking after a wayward brother in mother's will

Strange math syntax in old basic listing

Looking for an old image of designing a cpu with plan laid out / being edited on a literal floor

The most awesome army: 80 men left and 81 returned. Is it true?

Slide Partition from Rowstore to Columnstore

How do I get a cleat that's stuck in a pedal, detached from the shoe, out?

Explain Ant-Man's "not it" scene from Avengers: Endgame

Infinitely many hats

Is there a term for this?

How to write a vulnerable moment without it seeming cliche or mushy?

Is there any Biblical Basis for 400 years of silence between Old and New Testament?

Cryptography and patents

Orientable with respect to complex cobordism?

Why is Colorado so different politically from nearby states?

Applicants clearly not having the skills they advertise

What are the problems in teaching guitar via Skype?

How can I grammatically understand "Wir über uns"?

Singlequote and backslash

Can you use a concentration spell while using Mantle of Majesty?

Why is there a need to modify system call tables in Linux?

Have powerful mythological heroes ever run away or been deeply afraid?

How should I push back against my job assigning "homework"?

Why don't I have ground wiring on any of my outlets?

Is having a hidden directory under /etc safe?



Refactoring from Hooks to a class component


How to change an element's class with JavaScript?How do I remove a property from a JavaScript object?How do I remove a particular element from an array in JavaScript?How do I return the response from an asynchronous call?Refactor a React component from function to ES6 classReact functional stateless component, PureComponent, Component; what are the differences and when should we use what?How to turn this Component Class into a pure function Stateless Component (Typescript + React)Converting my class-based component into functional components using hooksHow does useState() in react retrieve the correct state object and function for a functional component when using the state hook?How to use React Hooks to refactor existing code?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















Can you please help with refactoring a bit of code from React Hooks to a class component? I'm new in React and this gives me a hard time. I know that useState provides some "getter" and "setter", but don't know how to refactor it to a state with props in a "typical" React class component.



Hooks:



export default function App() 
const [counter, setCounter] = useState([]);



React:



class App extends React.Component {
state =
counter:










share|improve this question






























    0















    Can you please help with refactoring a bit of code from React Hooks to a class component? I'm new in React and this gives me a hard time. I know that useState provides some "getter" and "setter", but don't know how to refactor it to a state with props in a "typical" React class component.



    Hooks:



    export default function App() 
    const [counter, setCounter] = useState([]);



    React:



    class App extends React.Component {
    state =
    counter:










    share|improve this question


























      0












      0








      0








      Can you please help with refactoring a bit of code from React Hooks to a class component? I'm new in React and this gives me a hard time. I know that useState provides some "getter" and "setter", but don't know how to refactor it to a state with props in a "typical" React class component.



      Hooks:



      export default function App() 
      const [counter, setCounter] = useState([]);



      React:



      class App extends React.Component {
      state =
      counter:










      share|improve this question
















      Can you please help with refactoring a bit of code from React Hooks to a class component? I'm new in React and this gives me a hard time. I know that useState provides some "getter" and "setter", but don't know how to refactor it to a state with props in a "typical" React class component.



      Hooks:



      export default function App() 
      const [counter, setCounter] = useState([]);



      React:



      class App extends React.Component {
      state =
      counter:







      javascript reactjs react-hooks






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Apr 7 at 16:56







      ridingTom

















      asked Mar 24 at 11:13









      ridingTomridingTom

      114




      114






















          2 Answers
          2






          active

          oldest

          votes


















          0














          From the react Hooks FAQ page:




          Should I use Hooks, classes, or a mix of both?

          When you’re ready, we’d encourage you to start trying Hooks in new components you
          write. Make sure everyone on your team is on board with using them and
          familiar with this documentation. We don’t recommend rewriting your
          existing classes to Hooks unless you planned to rewrite them anyway
          (e.g. to fix bugs).



          You can’t use Hooks inside of a class component, but you can
          definitely mix classes and function components with Hooks in a single
          tree. Whether a component is a class or a function that uses Hooks is
          an implementation detail of that component. In the longer term, we
          expect Hooks to be the primary way people write React components
          .




          to answer your question, the equivalent class component would be:



          class App extends React.Component 
          state =
          counter: [] // equivalent of useState([]);

          ...
          this.setState(prevState => (
          counter: [...prevState.counter, newelement]
          )) // equivalent of setCounter(counter => [...counter, newelement]);






          share|improve this answer






























            1














            You can take a look into this example. This a typical class components for increment/counting .



            class App extends React.Component 
            state = count: 0

            increment = () =>
            this.setState(
            count: this.state.count + 1
            );


            render()
            return(
            <button onClick=this.increment>+</button>
            );




            export default App;


            Here is the Hooks implementation of it.



             function App()
            const [count, setCount] = useState(0);

            const increment = () =>
            setCount(count+1);
            ;

            return(
            <button onClick=increment>+</button>
            );

            export default App;





            share|improve this answer























              Your Answer






              StackExchange.ifUsing("editor", function ()
              StackExchange.using("externalEditor", function ()
              StackExchange.using("snippets", function ()
              StackExchange.snippets.init();
              );
              );
              , "code-snippets");

              StackExchange.ready(function()
              var channelOptions =
              tags: "".split(" "),
              id: "1"
              ;
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function()
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled)
              StackExchange.using("snippets", function()
              createEditor();
              );

              else
              createEditor();

              );

              function createEditor()
              StackExchange.prepareEditor(
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader:
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              ,
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              );



              );













              draft saved

              draft discarded


















              StackExchange.ready(
              function ()
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55323208%2frefactoring-from-hooks-to-a-class-component%23new-answer', 'question_page');

              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              0














              From the react Hooks FAQ page:




              Should I use Hooks, classes, or a mix of both?

              When you’re ready, we’d encourage you to start trying Hooks in new components you
              write. Make sure everyone on your team is on board with using them and
              familiar with this documentation. We don’t recommend rewriting your
              existing classes to Hooks unless you planned to rewrite them anyway
              (e.g. to fix bugs).



              You can’t use Hooks inside of a class component, but you can
              definitely mix classes and function components with Hooks in a single
              tree. Whether a component is a class or a function that uses Hooks is
              an implementation detail of that component. In the longer term, we
              expect Hooks to be the primary way people write React components
              .




              to answer your question, the equivalent class component would be:



              class App extends React.Component 
              state =
              counter: [] // equivalent of useState([]);

              ...
              this.setState(prevState => (
              counter: [...prevState.counter, newelement]
              )) // equivalent of setCounter(counter => [...counter, newelement]);






              share|improve this answer



























                0














                From the react Hooks FAQ page:




                Should I use Hooks, classes, or a mix of both?

                When you’re ready, we’d encourage you to start trying Hooks in new components you
                write. Make sure everyone on your team is on board with using them and
                familiar with this documentation. We don’t recommend rewriting your
                existing classes to Hooks unless you planned to rewrite them anyway
                (e.g. to fix bugs).



                You can’t use Hooks inside of a class component, but you can
                definitely mix classes and function components with Hooks in a single
                tree. Whether a component is a class or a function that uses Hooks is
                an implementation detail of that component. In the longer term, we
                expect Hooks to be the primary way people write React components
                .




                to answer your question, the equivalent class component would be:



                class App extends React.Component 
                state =
                counter: [] // equivalent of useState([]);

                ...
                this.setState(prevState => (
                counter: [...prevState.counter, newelement]
                )) // equivalent of setCounter(counter => [...counter, newelement]);






                share|improve this answer

























                  0












                  0








                  0







                  From the react Hooks FAQ page:




                  Should I use Hooks, classes, or a mix of both?

                  When you’re ready, we’d encourage you to start trying Hooks in new components you
                  write. Make sure everyone on your team is on board with using them and
                  familiar with this documentation. We don’t recommend rewriting your
                  existing classes to Hooks unless you planned to rewrite them anyway
                  (e.g. to fix bugs).



                  You can’t use Hooks inside of a class component, but you can
                  definitely mix classes and function components with Hooks in a single
                  tree. Whether a component is a class or a function that uses Hooks is
                  an implementation detail of that component. In the longer term, we
                  expect Hooks to be the primary way people write React components
                  .




                  to answer your question, the equivalent class component would be:



                  class App extends React.Component 
                  state =
                  counter: [] // equivalent of useState([]);

                  ...
                  this.setState(prevState => (
                  counter: [...prevState.counter, newelement]
                  )) // equivalent of setCounter(counter => [...counter, newelement]);






                  share|improve this answer













                  From the react Hooks FAQ page:




                  Should I use Hooks, classes, or a mix of both?

                  When you’re ready, we’d encourage you to start trying Hooks in new components you
                  write. Make sure everyone on your team is on board with using them and
                  familiar with this documentation. We don’t recommend rewriting your
                  existing classes to Hooks unless you planned to rewrite them anyway
                  (e.g. to fix bugs).



                  You can’t use Hooks inside of a class component, but you can
                  definitely mix classes and function components with Hooks in a single
                  tree. Whether a component is a class or a function that uses Hooks is
                  an implementation detail of that component. In the longer term, we
                  expect Hooks to be the primary way people write React components
                  .




                  to answer your question, the equivalent class component would be:



                  class App extends React.Component 
                  state =
                  counter: [] // equivalent of useState([]);

                  ...
                  this.setState(prevState => (
                  counter: [...prevState.counter, newelement]
                  )) // equivalent of setCounter(counter => [...counter, newelement]);







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Mar 24 at 12:08









                  FractionFraction

                  1,198214




                  1,198214























                      1














                      You can take a look into this example. This a typical class components for increment/counting .



                      class App extends React.Component 
                      state = count: 0

                      increment = () =>
                      this.setState(
                      count: this.state.count + 1
                      );


                      render()
                      return(
                      <button onClick=this.increment>+</button>
                      );




                      export default App;


                      Here is the Hooks implementation of it.



                       function App()
                      const [count, setCount] = useState(0);

                      const increment = () =>
                      setCount(count+1);
                      ;

                      return(
                      <button onClick=increment>+</button>
                      );

                      export default App;





                      share|improve this answer



























                        1














                        You can take a look into this example. This a typical class components for increment/counting .



                        class App extends React.Component 
                        state = count: 0

                        increment = () =>
                        this.setState(
                        count: this.state.count + 1
                        );


                        render()
                        return(
                        <button onClick=this.increment>+</button>
                        );




                        export default App;


                        Here is the Hooks implementation of it.



                         function App()
                        const [count, setCount] = useState(0);

                        const increment = () =>
                        setCount(count+1);
                        ;

                        return(
                        <button onClick=increment>+</button>
                        );

                        export default App;





                        share|improve this answer

























                          1












                          1








                          1







                          You can take a look into this example. This a typical class components for increment/counting .



                          class App extends React.Component 
                          state = count: 0

                          increment = () =>
                          this.setState(
                          count: this.state.count + 1
                          );


                          render()
                          return(
                          <button onClick=this.increment>+</button>
                          );




                          export default App;


                          Here is the Hooks implementation of it.



                           function App()
                          const [count, setCount] = useState(0);

                          const increment = () =>
                          setCount(count+1);
                          ;

                          return(
                          <button onClick=increment>+</button>
                          );

                          export default App;





                          share|improve this answer













                          You can take a look into this example. This a typical class components for increment/counting .



                          class App extends React.Component 
                          state = count: 0

                          increment = () =>
                          this.setState(
                          count: this.state.count + 1
                          );


                          render()
                          return(
                          <button onClick=this.increment>+</button>
                          );




                          export default App;


                          Here is the Hooks implementation of it.



                           function App()
                          const [count, setCount] = useState(0);

                          const increment = () =>
                          setCount(count+1);
                          ;

                          return(
                          <button onClick=increment>+</button>
                          );

                          export default App;






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Mar 24 at 11:43









                          TRomeshTRomesh

                          1,92332236




                          1,92332236



























                              draft saved

                              draft discarded
















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid


                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.

                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55323208%2frefactoring-from-hooks-to-a-class-component%23new-answer', 'question_page');

                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                              SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

                              은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현