Using a functional component within a class












0















I'm wondering how I can create a stateless component within a class. Like if I use these functions outside the class, my page renders, but when I put them in the class. My page doesn't render. I want them to be inside the class so I can apply some class props to them.



class helloClass extends React.Component {
state = {
};
Hello =({ items}) => {
return (
<ul>
{items.map((item, ind) => (
<RenderHello
value={item.name}
/>
))}
</ul>
);
}

RenderHello = ({ value }) => {
return (
<div>
{open && value && (
<Hello
value={value}
/>
)}
</div>
);
}
render() {

}
}

export default (helloClass);


I have a setup like this. But not actually like this. And I keep getting the error that Hello and RenderHello do not exist. However, if I turn these into functions outside of the class, they work and everything renders on my page. I just want to know how I can achieve the same but within a class. If that's even possible.










share|improve this question




















  • 2





    Hi there. Can you please edit the code to be more like your actual setup? For example, seeing how your render method is implemented sounds pretty critical to solving this question.

    – Matthew Herbst
    Nov 15 '18 at 2:27











  • Is there a reason you want a stateless component inside a class component ?

    – Edwin Harly
    Nov 15 '18 at 2:55
















0















I'm wondering how I can create a stateless component within a class. Like if I use these functions outside the class, my page renders, but when I put them in the class. My page doesn't render. I want them to be inside the class so I can apply some class props to them.



class helloClass extends React.Component {
state = {
};
Hello =({ items}) => {
return (
<ul>
{items.map((item, ind) => (
<RenderHello
value={item.name}
/>
))}
</ul>
);
}

RenderHello = ({ value }) => {
return (
<div>
{open && value && (
<Hello
value={value}
/>
)}
</div>
);
}
render() {

}
}

export default (helloClass);


I have a setup like this. But not actually like this. And I keep getting the error that Hello and RenderHello do not exist. However, if I turn these into functions outside of the class, they work and everything renders on my page. I just want to know how I can achieve the same but within a class. If that's even possible.










share|improve this question




















  • 2





    Hi there. Can you please edit the code to be more like your actual setup? For example, seeing how your render method is implemented sounds pretty critical to solving this question.

    – Matthew Herbst
    Nov 15 '18 at 2:27











  • Is there a reason you want a stateless component inside a class component ?

    – Edwin Harly
    Nov 15 '18 at 2:55














0












0








0








I'm wondering how I can create a stateless component within a class. Like if I use these functions outside the class, my page renders, but when I put them in the class. My page doesn't render. I want them to be inside the class so I can apply some class props to them.



class helloClass extends React.Component {
state = {
};
Hello =({ items}) => {
return (
<ul>
{items.map((item, ind) => (
<RenderHello
value={item.name}
/>
))}
</ul>
);
}

RenderHello = ({ value }) => {
return (
<div>
{open && value && (
<Hello
value={value}
/>
)}
</div>
);
}
render() {

}
}

export default (helloClass);


I have a setup like this. But not actually like this. And I keep getting the error that Hello and RenderHello do not exist. However, if I turn these into functions outside of the class, they work and everything renders on my page. I just want to know how I can achieve the same but within a class. If that's even possible.










share|improve this question
















I'm wondering how I can create a stateless component within a class. Like if I use these functions outside the class, my page renders, but when I put them in the class. My page doesn't render. I want them to be inside the class so I can apply some class props to them.



class helloClass extends React.Component {
state = {
};
Hello =({ items}) => {
return (
<ul>
{items.map((item, ind) => (
<RenderHello
value={item.name}
/>
))}
</ul>
);
}

RenderHello = ({ value }) => {
return (
<div>
{open && value && (
<Hello
value={value}
/>
)}
</div>
);
}
render() {

}
}

export default (helloClass);


I have a setup like this. But not actually like this. And I keep getting the error that Hello and RenderHello do not exist. However, if I turn these into functions outside of the class, they work and everything renders on my page. I just want to know how I can achieve the same but within a class. If that's even possible.







javascript reactjs






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 5:04









dotconnor

1,107220




1,107220










asked Nov 15 '18 at 2:22









AnneJodayAnneJoday

246




246








  • 2





    Hi there. Can you please edit the code to be more like your actual setup? For example, seeing how your render method is implemented sounds pretty critical to solving this question.

    – Matthew Herbst
    Nov 15 '18 at 2:27











  • Is there a reason you want a stateless component inside a class component ?

    – Edwin Harly
    Nov 15 '18 at 2:55














  • 2





    Hi there. Can you please edit the code to be more like your actual setup? For example, seeing how your render method is implemented sounds pretty critical to solving this question.

    – Matthew Herbst
    Nov 15 '18 at 2:27











  • Is there a reason you want a stateless component inside a class component ?

    – Edwin Harly
    Nov 15 '18 at 2:55








2




2





Hi there. Can you please edit the code to be more like your actual setup? For example, seeing how your render method is implemented sounds pretty critical to solving this question.

– Matthew Herbst
Nov 15 '18 at 2:27





Hi there. Can you please edit the code to be more like your actual setup? For example, seeing how your render method is implemented sounds pretty critical to solving this question.

– Matthew Herbst
Nov 15 '18 at 2:27













Is there a reason you want a stateless component inside a class component ?

– Edwin Harly
Nov 15 '18 at 2:55





Is there a reason you want a stateless component inside a class component ?

– Edwin Harly
Nov 15 '18 at 2:55












2 Answers
2






active

oldest

votes


















2














Several ways of doing it, but the cleanist is to separate the stateless functions into it's their own files and have a single container that handles state and props and passes them down to the children:



Hello.js (displays the li items)



import React from 'react';

export default ({ items }) => (
<ul>
{items.map((item, ind) => (
<li key={ind}>
{item.name}
</li>
))}
</ul>
);


RenderHello.js (only returns Hello if open and value are true)



 import React from 'react';
import Hello from './Hello';

export default ({ open, value, items }) => (
open && value
? <Hello items={items} />
: null
);


HelloContainer.js (contains state and methods to update the children nodes)



 import React, { Component } from 'react';
import RenderHello from './RenderHello';

class HelloContainer extends Component {
state = {
items: [...],
open: false,
value: ''
};

...methods that update the state defined above (ideally, these would be passed down and triggered by the child component defined below)

render = () => <RenderHello {...this.state} />
}





share|improve this answer


























  • Thank you, this is exactly the solution I needed!

    – AnneJoday
    Nov 15 '18 at 3:33



















0














Its strange because you have a recursive call that will end up in a infinite loop, but syntactically, it would be something like that:



class helloClass extends React.Component {
state = {
};
Hello(items) {
return (
<ul>
{items.map((item, ind) => (
{this.RenderHello(item.name)}

))}
</ul>
);

}

RenderHello(value) {
return (
<div>

{open && value && (
{this.Hello(value)}
)}
</div>
);
}
render()

{

}
}

export default (helloClass);





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%2f53311520%2fusing-a-functional-component-within-a-class%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









    2














    Several ways of doing it, but the cleanist is to separate the stateless functions into it's their own files and have a single container that handles state and props and passes them down to the children:



    Hello.js (displays the li items)



    import React from 'react';

    export default ({ items }) => (
    <ul>
    {items.map((item, ind) => (
    <li key={ind}>
    {item.name}
    </li>
    ))}
    </ul>
    );


    RenderHello.js (only returns Hello if open and value are true)



     import React from 'react';
    import Hello from './Hello';

    export default ({ open, value, items }) => (
    open && value
    ? <Hello items={items} />
    : null
    );


    HelloContainer.js (contains state and methods to update the children nodes)



     import React, { Component } from 'react';
    import RenderHello from './RenderHello';

    class HelloContainer extends Component {
    state = {
    items: [...],
    open: false,
    value: ''
    };

    ...methods that update the state defined above (ideally, these would be passed down and triggered by the child component defined below)

    render = () => <RenderHello {...this.state} />
    }





    share|improve this answer


























    • Thank you, this is exactly the solution I needed!

      – AnneJoday
      Nov 15 '18 at 3:33
















    2














    Several ways of doing it, but the cleanist is to separate the stateless functions into it's their own files and have a single container that handles state and props and passes them down to the children:



    Hello.js (displays the li items)



    import React from 'react';

    export default ({ items }) => (
    <ul>
    {items.map((item, ind) => (
    <li key={ind}>
    {item.name}
    </li>
    ))}
    </ul>
    );


    RenderHello.js (only returns Hello if open and value are true)



     import React from 'react';
    import Hello from './Hello';

    export default ({ open, value, items }) => (
    open && value
    ? <Hello items={items} />
    : null
    );


    HelloContainer.js (contains state and methods to update the children nodes)



     import React, { Component } from 'react';
    import RenderHello from './RenderHello';

    class HelloContainer extends Component {
    state = {
    items: [...],
    open: false,
    value: ''
    };

    ...methods that update the state defined above (ideally, these would be passed down and triggered by the child component defined below)

    render = () => <RenderHello {...this.state} />
    }





    share|improve this answer


























    • Thank you, this is exactly the solution I needed!

      – AnneJoday
      Nov 15 '18 at 3:33














    2












    2








    2







    Several ways of doing it, but the cleanist is to separate the stateless functions into it's their own files and have a single container that handles state and props and passes them down to the children:



    Hello.js (displays the li items)



    import React from 'react';

    export default ({ items }) => (
    <ul>
    {items.map((item, ind) => (
    <li key={ind}>
    {item.name}
    </li>
    ))}
    </ul>
    );


    RenderHello.js (only returns Hello if open and value are true)



     import React from 'react';
    import Hello from './Hello';

    export default ({ open, value, items }) => (
    open && value
    ? <Hello items={items} />
    : null
    );


    HelloContainer.js (contains state and methods to update the children nodes)



     import React, { Component } from 'react';
    import RenderHello from './RenderHello';

    class HelloContainer extends Component {
    state = {
    items: [...],
    open: false,
    value: ''
    };

    ...methods that update the state defined above (ideally, these would be passed down and triggered by the child component defined below)

    render = () => <RenderHello {...this.state} />
    }





    share|improve this answer















    Several ways of doing it, but the cleanist is to separate the stateless functions into it's their own files and have a single container that handles state and props and passes them down to the children:



    Hello.js (displays the li items)



    import React from 'react';

    export default ({ items }) => (
    <ul>
    {items.map((item, ind) => (
    <li key={ind}>
    {item.name}
    </li>
    ))}
    </ul>
    );


    RenderHello.js (only returns Hello if open and value are true)



     import React from 'react';
    import Hello from './Hello';

    export default ({ open, value, items }) => (
    open && value
    ? <Hello items={items} />
    : null
    );


    HelloContainer.js (contains state and methods to update the children nodes)



     import React, { Component } from 'react';
    import RenderHello from './RenderHello';

    class HelloContainer extends Component {
    state = {
    items: [...],
    open: false,
    value: ''
    };

    ...methods that update the state defined above (ideally, these would be passed down and triggered by the child component defined below)

    render = () => <RenderHello {...this.state} />
    }






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 15 '18 at 4:21

























    answered Nov 15 '18 at 2:56









    Matt CarlottaMatt Carlotta

    2,6071612




    2,6071612













    • Thank you, this is exactly the solution I needed!

      – AnneJoday
      Nov 15 '18 at 3:33



















    • Thank you, this is exactly the solution I needed!

      – AnneJoday
      Nov 15 '18 at 3:33

















    Thank you, this is exactly the solution I needed!

    – AnneJoday
    Nov 15 '18 at 3:33





    Thank you, this is exactly the solution I needed!

    – AnneJoday
    Nov 15 '18 at 3:33













    0














    Its strange because you have a recursive call that will end up in a infinite loop, but syntactically, it would be something like that:



    class helloClass extends React.Component {
    state = {
    };
    Hello(items) {
    return (
    <ul>
    {items.map((item, ind) => (
    {this.RenderHello(item.name)}

    ))}
    </ul>
    );

    }

    RenderHello(value) {
    return (
    <div>

    {open && value && (
    {this.Hello(value)}
    )}
    </div>
    );
    }
    render()

    {

    }
    }

    export default (helloClass);





    share|improve this answer






























      0














      Its strange because you have a recursive call that will end up in a infinite loop, but syntactically, it would be something like that:



      class helloClass extends React.Component {
      state = {
      };
      Hello(items) {
      return (
      <ul>
      {items.map((item, ind) => (
      {this.RenderHello(item.name)}

      ))}
      </ul>
      );

      }

      RenderHello(value) {
      return (
      <div>

      {open && value && (
      {this.Hello(value)}
      )}
      </div>
      );
      }
      render()

      {

      }
      }

      export default (helloClass);





      share|improve this answer




























        0












        0








        0







        Its strange because you have a recursive call that will end up in a infinite loop, but syntactically, it would be something like that:



        class helloClass extends React.Component {
        state = {
        };
        Hello(items) {
        return (
        <ul>
        {items.map((item, ind) => (
        {this.RenderHello(item.name)}

        ))}
        </ul>
        );

        }

        RenderHello(value) {
        return (
        <div>

        {open && value && (
        {this.Hello(value)}
        )}
        </div>
        );
        }
        render()

        {

        }
        }

        export default (helloClass);





        share|improve this answer















        Its strange because you have a recursive call that will end up in a infinite loop, but syntactically, it would be something like that:



        class helloClass extends React.Component {
        state = {
        };
        Hello(items) {
        return (
        <ul>
        {items.map((item, ind) => (
        {this.RenderHello(item.name)}

        ))}
        </ul>
        );

        }

        RenderHello(value) {
        return (
        <div>

        {open && value && (
        {this.Hello(value)}
        )}
        </div>
        );
        }
        render()

        {

        }
        }

        export default (helloClass);






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 15 '18 at 2:37

























        answered Nov 15 '18 at 2:32









        RobsonsjreRobsonsjre

        971615




        971615






























            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%2f53311520%2fusing-a-functional-component-within-a-class%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

            Florida Star v. B. J. F.

            Danny Elfman

            Lugert, Oklahoma