Menu

Would You Rather Have a Dumb Component or a Smart Component?
πŸ“°

Would You Rather Have a Dumb Component or a Smart Component?

DEV Community: frontendΒ·Byte CornerΒ·18 days ago
#sPyC9cG2
#devtofrontend#devto#dev#component#components
Reading 0:00
15s threshold

A Component Doesn't Have to Be Dumb to Be Reusable

There's a long-running idea in frontend architecture:

Keep components dumb.

Let them receive data, render the UI, and leave everything else to something higher up the tree.

It sounds clean. And often, it is.

But there's a trade-off we don't always consider:

Making a component simpler can make the surrounding architecture more complicated.

Let's look at both approaches.

πŸ”΄ The Problem

Imagine a component that displays a list of users.

It needs to fetch the data, filter the results, and render them.

One approach is to put everything inside UserList .

function UserList () { const [ users , setUsers ] = useState < User [] > ([]); const [ query , setQuery ] = useState ( "" );

useEffect (() => { getUsers (). then ( setUsers ); }, []);

const filteredUsers = useMemo ( () => users . filter (( user ) => user . name . includes ( query )), [ users , query ], );

return ( < section > < input value = { query } onChange = { ( event ) => setQuery ( event .…

Continue reading β€” create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.