R
R
Roman Yakimchuk2019-11-23 16:47:54
JavaScript
Roman Yakimchuk, 2019-11-23 16:47:54

How to beautifully modify the behavior of one component in React?

Friends,
the question in a million. I'm new to React, can't find an answer yet.
There is a task, for example, to hang conditions and modifiers on the link. For example, it should be available only under condition No. 1, blue under condition No. 2, should cause some kind of validation when clicked, should send data to statistics when clicked.
You need to decide without copying the code and linking to the style in the source code or template, these cases should be customizable for each link.
With JSX, I would write like this (crooked, not a solution):

<a class={ (blue ? 'blue' : '') + ' ' + (disabled ? 'disabled' : '')} onClick = {validate() && send() }>Ссылка</a>

What I want to do is to reuse:
<a blue={false} disabled={false} send-stat-on-click validate-on-click>Ссылка</a>

Angular and Vue have directives, everything is beautiful there, but with react, some kind of mess comes out of components and inline logic. I do not believe that everything is so bad in React.
How do you generally solve such problems?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Dozmorov, 2019-11-24
@Dozalex

The link component is simply created, the necessary props are thrown into it (disabled, onClick, and others). Well, in the component itself you use them.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question