<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: neil-morgan</title>
    <description>The latest articles on DEV Community by neil-morgan (@neilmorgan).</description>
    <link>https://dev.to/neilmorgan</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F307813%2Fb786c00a-862d-49de-95a7-6d7aa688e687.png</url>
      <title>DEV Community: neil-morgan</title>
      <link>https://dev.to/neilmorgan</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/neilmorgan"/>
    <language>en</language>
    <item>
      <title>React Context and Provider to help handle scroll and viewport visibility events</title>
      <dc:creator>neil-morgan</dc:creator>
      <pubDate>Tue, 05 May 2020 11:54:24 +0000</pubDate>
      <link>https://dev.to/neilmorgan/react-context-and-provider-to-help-handle-scroll-and-viewport-visibility-events-dcj</link>
      <guid>https://dev.to/neilmorgan/react-context-and-provider-to-help-handle-scroll-and-viewport-visibility-events-dcj</guid>
      <description>&lt;p&gt;I've run into a roadblock when putting together the below process.&lt;br&gt;
I'm trying to create a context which will globally provide window properties to components so that the components can react to window changes (scroll, resize, etc).&lt;/p&gt;

&lt;p&gt;I'm doing this so that rather than each component running their own set of event listeners. I only have to run one set and pass the results down.&lt;/p&gt;

&lt;p&gt;So far the context adds the scroll and resize listeners, throttles them and makes the values available to the provider wrapper. All works fine, and the values are available in the wrapped component.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//WindowContext.js

import React, { Component, createContext } from "react"
import throttle from "lodash.throttle"

export const WindowContext = createContext()

class WindowContextProvider extends Component {
  constructor(props) {
    super(props)
    this.state = {
      windowScrollY: null,
      windowInnerHeight: null,
    }
    this.throttledFunction = throttle(this.updateState, 500)
  }

  componentDidMount() {
    window.addEventListener("scroll", this.throttledFunction, false)
    window.addEventListener("resize", this.throttledFunction, false)
  }
  componentWillUnmount() {
    window.removeEventListener("scroll", this.throttledFunction, false)
    window.removeEventListener("resize", this.throttledFunction, false)
  }

  updateState = () =&amp;gt; {
    this.setState({
      windowScrollY: window.scrollY,
      windowInnerHeight: window.innerHeight,
    })
  }

  render() {
    return (
      &amp;lt;WindowContext.Provider value={{ ...this.state }}&amp;gt;
        {this.props.children}
      &amp;lt;/WindowContext.Provider&amp;gt;
    )
  }
}

export default WindowContextProvider
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;





&lt;h2&gt;
  
  
  The problem
&lt;/h2&gt;

&lt;p&gt;In &lt;code&gt;SomeComponent.js&lt;/code&gt; below you can see what it is what I am attempting. It has two fundamental problems.&lt;/p&gt;

&lt;p&gt;I cannot (shouldn't) set state within render so I'm not sure how I can set &lt;code&gt;isActive&lt;/code&gt; within the state to true (so that some ternary operator could utilise it). And the element coordinates I gather in the componentDidMount are static meaning I cant match them to scroll or inner height to determine if the element is visible.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//SomeComponent.js

import React from "react"
import ReactDOM from "react-dom"

import { WindowContext } from "../contexts/WindowContext"

class DevTools extends React.Component {
  constructor(props) {
    super(props)
    this.state = {
      isActive: false, //want to set isActive:true if 
      element: null,
    }
  }
  componentDidMount = () =&amp;gt; {
    this.setState({
      element: ReactDOM.findDOMNode(this).getBoundingClientRect(),
    })
  }

  render() {
    return (
      &amp;lt;WindowContext.Consumer&amp;gt;
        {context =&amp;gt; {
          //THE CRUX OF THE PROBLEM
          const { windowScrollY, windowInnerHeight } = context
          console.log(windowScrollY) //tracked
          console.log(windowInnerHeight) //tracked
          console.log(this.state.element) //not tracked
          //this.state.element is currently static as it is simply set on mount, needs to be tracked
          //adding an event listener would defeat the point of creating the context

          //Below not an option, cannot set state within render, not sure how else to manage toggling when the element is visible
          handleWindowChange = () =&amp;gt; { 
            if (this.state.element.top + 100 &amp;lt; windowInnerHeight &amp;amp;&amp;amp; this.state.element.bottom &amp;gt;= 0) {
              this.setState({
                isActive: true,
              })
            }
          }
          return (
          //some div with a ternary operator

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;I'm trying to come up with a solution which both GLOBALLY tracks the event listeners (scroll, resize) but also tracks the position/visibility of any component the provider wraps.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;So maybe something in which I can pass all components that the context provider wraps up to the context state and handle matching their visibility there instead. But I'm struggling to wrap my head around where to start&lt;/p&gt;

</description>
      <category>react</category>
      <category>reactnative</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
