REACT_LIB Telegram 709
Сегодня хочу показать вам один из практичных паттернов в React - Container & Presentational Components.

Очень часто мы мешаем бизнес-логику и UI в одном компоненте. Это делает код тяжелым для поддержки, особенно в больших проектах.

⚡️ Подход простой:

- Container-компоненты отвечают за работу с данными: запросы к API, обработку состояния, вызовы хендлеров.
- Presentational-компоненты - только за отображение. Они получают данные и колбэки через props и не думают о том, откуда эти данные пришли.

Пример 👇


// Container
function UserContainer() {
const [user, setUser] = React.useState(null);

React.useEffect(() => {
fetch("/api/user")
.then(res => res.json())
.then(setUser);
}, []);

return <UserProfile user={user} />;
}

// Presentational
function UserProfile({ user }) {
if (!user) return <p>Загрузка...</p>;
return <h2>{user.name}</h2>;
}


👉 Такой подход облегчает тестирование, переиспользование компонентов и уменьшает связность.

А как вы пишете? Делите логику и UI или чаще держите все в одном компоненте?

✍️ @React_lib
👍3



tgoop.com/React_lib/709
Create:
Last Update:

Сегодня хочу показать вам один из практичных паттернов в React - Container & Presentational Components.

Очень часто мы мешаем бизнес-логику и UI в одном компоненте. Это делает код тяжелым для поддержки, особенно в больших проектах.

⚡️ Подход простой:

- Container-компоненты отвечают за работу с данными: запросы к API, обработку состояния, вызовы хендлеров.
- Presentational-компоненты - только за отображение. Они получают данные и колбэки через props и не думают о том, откуда эти данные пришли.

Пример 👇


// Container
function UserContainer() {
const [user, setUser] = React.useState(null);

React.useEffect(() => {
fetch("/api/user")
.then(res => res.json())
.then(setUser);
}, []);

return <UserProfile user={user} />;
}

// Presentational
function UserProfile({ user }) {
if (!user) return <p>Загрузка...</p>;
return <h2>{user.name}</h2>;
}


👉 Такой подход облегчает тестирование, переиспользование компонентов и уменьшает связность.

А как вы пишете? Делите логику и UI или чаще держите все в одном компоненте?

✍️ @React_lib

BY React


Share with your friend now:
tgoop.com/React_lib/709

View MORE
Open in Telegram


Telegram News

Date: |

On June 7, Perekopsky met with Brazilian President Jair Bolsonaro, an avid user of the platform. According to the firm's VP, the main subject of the meeting was "freedom of expression." Private channels are only accessible to subscribers and don’t appear in public searches. To join a private channel, you need to receive a link from the owner (administrator). A private channel is an excellent solution for companies and teams. You can also use this type of channel to write down personal notes, reflections, etc. By the way, you can make your private channel public at any moment. Hashtags are a fast way to find the correct information on social media. To put your content out there, be sure to add hashtags to each post. We have two intelligent tips to give you: Write your hashtags in the language of your target audience. The main design elements of your Telegram channel include a name, bio (brief description), and avatar. Your bio should be:
from us


Telegram React
FROM American