简体   繁体   English

在现有的Web项目中隐藏React组件

[英]Hide a React component in existing web project

I'm currently trying to integrate a React component into a legacy web project (a huge html index file and some js scripts using mostly jquery). 我目前正在尝试将React组件集成到旧版Web项目中(一个巨大的html索引文件和一些使用jQuery的js脚本)。

I am trying to add the rendered react component into a separate tab. 我试图将渲染的react组件添加到单独的选项卡中。 Now the problem is that I cannot control the visibility of the top level React component. 现在的问题是我无法控制顶级React组件的可见性。

Say I have the following src code in html: 说我在html中有以下src代码:

...
<div id="wrapper">
    <div id="react-component"></div>
</div>
...

I render the inner component using React. 我使用React渲染内部组件。 Now is there any way to control the visibility of either "#wrapper" or "#react-component" ? 现在有什么方法可以控制“ #wrapper”或“#react-component”的可见性吗? jquery does not help. jQuery没有帮助。

I am relatively new to React and it seems that integrating it into old project can be a huge pain. 我对React还是比较陌生,似乎将其集成到旧项目中可能会非常痛苦。

I render the inner component using React. 我使用React渲染内部组件。 Now is there any way to control the visibility of either "#wrapper" or "#react-component" ? 现在有什么方法可以控制“ #wrapper”或“#react-component”的可见性吗?

Well those are two different worlds. 好吧,这是两个不同的世界。

For the #wrapper you can just use DOM manipulation like $.hide() and $.show() or however you'd normally do it in jQuery. 对于#wrapper你可以只使用DOM操作像$.hide()$.show()或不过你通常会做它的jQuery。

For the #react-component you need to call ReactDOM.render() , and you can pass in a visible prop to change the visibility of the rendered elements. 对于#react-component您需要调用ReactDOM.render() ,并且可以传入一个visible prop来更改渲染元素的可见性。 Something like: 就像是:

ReactDOM.render(
  <MyComponent visible={isReactComponentVisible} />, 
  document.getElementById("react-component")
);

Now your React component can show or hide itself however you want. 现在,您的React组件可以根据需要显示或隐藏自身。

class MyComponent extends React.Component {
  render() {
    return (
      <div style={{ display: this.props.visible ? "block" : "none" }}>
        ...
      </div>
    )
  }
}

Of course its up to you to call ReactDOM.render() anytime that isReactComponentVisible changes. 当然, isReactComponentVisible更改,都可以调用ReactDOM.render() A formal state binding library like Redux could help you here, if the complexity warrants it. 如果复杂性允许的话,像Redux这样的正式状态绑定库可以为您提供帮助。

Remember that React will diff the renders so calling ReactDOM.render() is not rebuilding the entire component's DOM (as jQuery would be), just what changed (ie the DOM effected by the visible prop: the style.display attribute.) 请记住,React会ReactDOM.render()渲染,因此调用ReactDOM.render() 不会重建整个组件的DOM(就像jQuery一样),而只是重建(即,由visible道具影响的DOM: style.display属性)。

Create a js file and export a default function to help you rendering react components. 创建一个js文件并导出默认函数,以帮助您渲染React组件。

Like this 像这样

import React from 'react';
import ReactDOM from 'react-dom';
export default function(component, container , props = {}, callback){      

    React.createElement(component , props);
    ReactDOM.render(component, container, callback);

}

Your component will look like this 您的组件将如下所示

import React,{PropTypes} from 'react';

class MySampleComponent extends React.Component{

   static propTypes ={
      hide : PropTypes.bool
   }
   static defaultProps= {
      hide : false
   }

   render(){
      return(
         <div style={display : this.props.hide : 'none' : 'block'}> </div>

      );



   }

}

Import the above function to render your component in js file which you will add in index.html 导入上面的函数以在js文件中呈现组件,您将在index.html中添加

import render from './pathto/RenderHelper'
import MyComponent from './MyComponent' 

class IndexPage {

  constructor(){
     this.renderUI();
  }

  renderUI(){
    var container = document.getElementById('react-component');
    render(MyComponent, container, {hide : false});
  } 
}

Note you need to add your page.index.js file to webpack.config.js entry so that webpack can compile it, like this. 请注意,您需要将page.index.js文件添加到webpack.config.js条目中,以便webpack可以像这样编译它。

entry: { page.index : 'location/to/page.index.js' }

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM