0

I have a div tag I want to render only if renderCard() style overflow is scroll. I have tried a renderCard().style.overflow which does not seem to target this

Edit: renderCard added

  const SearchCard = () => (
    <button class="invisible-button" onClick={onSearchCardClick}>
      //
    </button>
  );

  const AnswerCard = () => (
    <div className="results-set">
      //
    </div>
  );

  const renderCard = () => {
    if (card && card.answer) {
      return AnswerCard();
    } else if (card) {
      return SearchCard();
    }
    return null;
  };
    <React.Fragment>
      <div id="search-results">{renderCard()}</div>
      {renderFollowup ? null : (
        <React.Fragment>
          <div id="search-footer">
            {
              (renderCard().style.overflow = "scroll" ? (
                <div className="scroll-button">
                  <a href="#bottomSection">
                    <img src="images/arrow_down.svg" alt="scroll to bottom" />
                  </a>
                </div>
              ) : null)
            }
          </div>
        </React.Fragment>
      )}
    </React.Fragment>
  );
};
1
  • 1
    Attach a react ref to the element being rendered in renderCard to get a reference to the DOM node and check its style attribute. Can you update your question with your renderCard code? Commented Jun 6, 2020 at 23:22

3 Answers 3

4

To do this you need a ref to the actual DOM element rendered by renderCard().

renderCard() here returns a React element which doesn't have the style property or any other DOM properties on it - it's just a React representation of what the DOM element will eventually be once rendered - hence you need to get the actual DOM element via a ref where you'll have access to this and other properties.

Example code below using useRef to create the ref that will be attached to the element with the style you need to access. Note how useEffect is used to access the ref's value because it's only available after the first render when the DOM element is present.

const Example = () => {
  const ref = React.useRef()
  
  React.useEffect(() => {
    alert('overflow value is: ' + ref.current.style.overflow)
  }, [])
  
  return (
    <div ref={ref} style={{ overflow: 'scroll' }}>hello world</div>
  )
}

ReactDOM.render(<Example />, document.getElementById('root'))
<script crossorigin src="https://unpkg.com/react@16/umd/react.production.min.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.production.min.js"></script>

<div id="root"></div>

Sign up to request clarification or add additional context in comments.

1 Comment

you should use a ref like this inside renderCard() to access the style.overflow of the relevant element
0

Refactor the cards to be actual rendered components, pass the ref and attach to the elements

const SearchCard = ({ overflowRef }) => (
  <button ref={overflowRef} class="invisible-button" onClick={onSearchCardClick}>
    //
  </button>
);

const AnswerCard = ({ overflowRef }) => (
  <div ref={overflowRef} className="results-set">
    //
  </div>
);

const RenderCard = ({ overflowRef }) => {
  if (card && card.answer) {
    return <AnswerCard overflowRef={overflowRef} />;
  } else if (card) {
    return <SearchCard overflowRef={overflowRef} />;
  }
  return null;
};

In the component rendering them, create a ref using either createRef or useRef react hook if it is a functional component

const overflowRef = createRef();

or

const overflowRef = useRef();

Pass the ref to RenderCard, to be passed on

<RenderCard overflowRef={overflowRef} />

And then check the overflow value as such

overflowRef.current.style.overflow === "scroll"

Comments

0

With the approach above you might want to refactor some of the HoC components and pass a function from parent to child that returns the ref of the element to be accessed also I am not sure of the scope of the block of code where you are executing the ternary.

Perhaps a simpler hack is to rely on using a useState hook with vanilla DOM selectors and passing that into the or even better, just add it to the stateless func component that wraps React.Fragment as a hook:

  const myComponent = () => {
      const [hasScrollOverflow, setHasScrollOverflow] = useState(false);

      React.useEffect(() => {
        const element = document.querySelector(".results-set");
        const elementStyle = element.style;

        if (elementStyle.getPropertyValue('overflow') === 'scroll') {
          setHasScrollOverflow(true);
        }

      }, [])
      return (
        <React.Fragment>
          <div id="search-results">{renderCard()}</div>
          {renderFollowup ? null : (
            <React.Fragment>
              <div id="search-footer">
                {hasScrollOverflow ? (
                  <div className="scroll-button">
                    <a href="#bottomSection">
                      <img src="images/arrow_down.svg" alt="scroll to bottom" />
                    </a>
                  </div>
                ) : null}
              </div>
            </React.Fragment>
          )}
        </React.Fragment>
      );
    };

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.