Coder Social home page Coder Social logo

react-dom-reflection's Introduction

react-dom-reflection

Experimental utils, to get more from React-Dom

Warning

This library uses details of the internal React-Dom realization. Be careful.

Use cases

Connect React and Dom trees

React tree is where your Application lives, but DOM tree is where it is actually exists. React.Portals are breaking connections between React and DOM, and while events would be automatically managed by React - everything else would not.

Get debug information

Sometimes you need it - get some react information out of dom tree. Get React instance from a Dom Node.

Find your place

It always was a right decision to keep Components in isolation, and not let them assess their parents. But it still could be useful in some cases.

API

The goal is to keep API not simple, but less fragile, to prevent potential breakages in the future.

instanceContainsNode(componentInstance, DomNode): boolean

Returns if DomNode is contained inside Component.

nodeContainsNode(hostNode, targetNode): boolean

Returns if targetNode is contained inside hostNode. This is the same as Dom API node.contains(anotherNode)

unsafe_portalsWithin(componentInstance): DomNodes[],

Returns all portals within

unsafe_childNodesWithin(componentInstance): DomNodes[],

Returns all direct children of component

unsafe_getFiberFromNode(DomNode): Fiber

Returns fiber(wrapped) for a node

Example

class MyComponent extends React.Component {
  componentDidMount() {
    document.body.addEventListener(
        'click',
        event => {
          if (instanceContainsNode(this, event.target)) {
            console.log('this event is somewhere inside me')
          } 
        }
    )    
  }

  render(){
    return (
      <React.Fragment>
        <div> local div </div>
        <Portal> portaled content </div> 
      </React.Fragment>
    )
  }
}

License

MIT

react-dom-reflection's People

Contributors

thekashey avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.