A
A
Anastasia2020-12-06 20:33:34
React
Anastasia, 2020-12-06 20:33:34

Why does the data from the store not fall into the condition for the button parameter when the component is first rendered?

The dateIndicator value comes from the store and is initially equal to 'M' - that is, the active class must initially be assigned to the M button by a ternary condition for the className parameter - which does not happen when the component is first rendered. The active class is not assigned to any element. In the future, when you press the event, it works correctly and the class is assigned. How to make it so that when the component is loaded, the active class is assigned to ?

5fcd157ad704d964283618.jpeg

export const ControlIndicators = ({ position }) => {
  const dispatch = useDispatch();
  const dateIndicator = useSelector(getDateIndicator);

  const clickHandler = value => {
    dispatch(setDateIndicator(value));
  };

  const buttons = [
    { title: 'Дневной показатель', value: 'Д' },
    { title: 'Месячный показатель', value: 'М' },
  ];

  return (
    <Control position={position} className="leaflet-bar-row">
      {buttons.map(item => {
        return (
          <button
            className={
              dateIndicator === item.value
                ? 'buttons-leaflet-row-item active'
                : 'buttons-leaflet-row-item'
            }
            title={item.title}
            value={item.value}
            onClick={() => clickHandler(item.value)}
          >
            {item.value === 'Д' ? 'Д' : 'М'}
          </button>
        );
      })}
    </Control>
  );
};

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir, 2020-12-07
@Casufi

Based on the information you provided, it is impossible to answer the question.
Here is an example without a state and it works there.
https://codesandbox.io/s/billowing-dream-d4r3c?fil...
Sandbox the issue and post a link.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question