Hacker News new | past | comments | ask | show | jobs | submit login

In some way, yes, the state belongs to the component itself, because in the end the component is an object and objects encapsulate that state as long as they are alive.

The beauty of "stateless" components is that you can see them as functions, and should aim to create them as pure functions[1] where the only thing that can change the output (HTML) of your component/function is its input.

Another way to see it is as syntactic sugar for pure functions where instead of:

MyMenu.render({name: 'Something', open: false});

MyMenu.render({name: 'Something', open: true});

You have

var menu = new MyMenu({name: 'Something', open: false});

menu.render();

// Then change the state:

menu.setOpen(true);

menu.render();

// Or maybe

menu.open(); // which calls setOpen and render

[1] https://en.wikipedia.org/wiki/Pure_function




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: