Access to internal function from externally loaded HTML in React component

前端 未结 1 1483
天命终不由人
天命终不由人 2021-01-19 10:55

I have the following use case.

Some HTML from a third party source is loaded inside my React component:

class MyComponent extends Component {
  rende         


        
1条回答
  •  生来不讨喜
    2021-01-19 11:15

    Using "dangerouslySetInnerHTML" is ..."dangerous" as its name ^^, which is actually not pure React way, either.

    However, If you have to do it, you can do something like this (take advantage of built-in jQuery inside React be default)

    =====

    EDITED VERSION FROM HERE: (use only 1 component)

    export default class MyComponent extends Component {
      componentDidMount() {
        // using jQuery to manipulate DOM element form third-party source
        // NB. you may think of using setTimeout here, to wait for the external source to be fully loaded here, of course it's not so safe
        // but anyway, we are using "dangerouslySetInnerHTML" anyway => quite dangerous, though ^^  
    
        // setTimeout(function(){   
          $(document.findElementsByTagName("span")[0]).click(function(e){ 
          // or perhaps $("#spanID").click if you can, just be careful between DOM element and react component
    
              e.preventDefault();
              // DO SOMETHING HERE, just like you do in the window.onload function
              // or maybe you also need to get param values by getting $(this).data("...") or $(this).attr("ATTRIBUTE-NAME")
              return false;
    
          });
        // });
      }
      render() {
        return (
          
    ); } }

    =====

    OLD ANSWER FROM HERE: (use 2 components)

    ParentComponent:

    export default class MyComponent extends Component {
      constructor(props) {
        super(props);
        this.callbackOnThisComponent = this.callbackOnThisComponent.bind(this);
      }
      callbackOnThisComponent(param1, param2, param3) {
        // do whatever you like with the above params
      }
      render() {
        return (
          
        );
      }
    }
    

    ChildComponent:

    export default class ChildComponent extends Component {
      componentDidMount() {
        // using jQuery to manipulate DOM element form third-party source
    
        let that = this;
    
        // NB. you may think of using setTimeout here, to wait for the external source to be fully loaded here, of course it's not so safe
        // but anyway, we are using "dangerouslySetInnerHTML" anyway => quite dangerous, though ^^  
    
        $(document.findElementsByTagName("span")[0]).click(function(e){ 
        // or perhaps $("#spanID").click if you can, just be careful between DOM element and react component
    
            e.preventDefault();
            that.props.triggerCallbackOnParent(param1, param2, param3);
            // or maybe you need to get param values by getting $(this).data("...") or $(this).attr("ATTRIBUTE-NAME")
            return false;
    
        }, that);
    
      }
      render() {
        return (
          
    ); } }

    I just use the main React's idea, which is passing props downward to children components, and when you want to trigger a function upward from child component, create a callback function on parent. For your or anyone else's reference, this is my demonstration on how to pass callback function from parent to multi-level-children components:

    Force React container to refresh data

    Re-initializing class on redirect

    if this doesn't work yet, feel free to show me some error logs, thanks

    0 讨论(0)
提交回复
热议问题