0

例如,像这样的组件:

import React, { Component } from 'react';
import BodyContent from './BodyContent';
import BottomOne from './BottomOne';
import BottomTwo from './BottomTwo';
class App extends Component {
  render() {
    return (
      <div className="App">
        <BodyContent />
        <BottomOne />
      </div>
    );
  }
}

export default App;

我想在BodyContent那个卸载BottomOne和安装上实现一个函数BottomTwo,所以当我激活这个函数时,代码被重组为:

import React, { Component } from 'react';
import BodyContent from './BodyContent';
import BottomOne from './BottomOne';
import BottomTwo from './BottomTwo';
class App extends Component {
  render() {
    return (
      <div className="App">
        <BodyContent />
        <BottomTwo />
      </div>
    );
  }
}

export default App;

我对 React 很陌生,所以如果有更好的方法,我愿意接受建议,但我真的需要最终结果,即在 BodyContent 上 unmountsBottomOne和 mounts的函数BottomTwo

4

4 回答 4

2

您可以维护一个状态,该状态告诉您要渲染哪个组件。大致是这样的

import React, { Component } from 'react';
import BodyContent from './BodyContent';
import BottomOne from './BottomOne';
import BottomTwo from './BottomTwo';
class App extends Component {
  changeBottomComponent = (comp) => {
    this.setState({ showBottom: comp})
  }
  render() {
    return (
      <div className="App">
        <BodyContent changeBottomComponent={this.changeBottomComponent}/>
        {this.state.showBottom === 1 ? <BottomOne /> : <BotttomTwo />}

      </div>
    );
  }
}

export default App;
于 2017-09-05T19:12:22.793 回答
1

您可以使用stateprops渲染不同的组件。

例子:

import React, {
    Component
}
from 'react';
import BodyContent from './BodyContent';
import BottomOne from './BottomOne';
import BottomTwo from './BottomTwo';
class App extends Component {
    constructor(props) {
        super(props);
        this.state = {
            decider: false
        };
    }
    render() {
        const bottomContent = this.state.decider === true ? <BottomOne /> : <BottomTwo />;
        return (
            <div className="App">
                <BodyContent />
                { bottomContent }
            </div>
        );
    }
}
export
default App;
于 2017-09-05T19:11:13.343 回答
1

您也可以直接使用状态中的组件并渲染它们。这种方式可以更灵活。

const BottomOne = () => <div>BottomOne</div>;
const BottomTwo = () => <div>BottomTwo</div>;

class Example extends React.Component {
  constructor() {
    super();
    this.state = { show: BottomOne };
    this.toggleComponent = this.toggleComponent.bind(this);
  }

  toggleComponent() {
    // Use whatever logic here to decide.
    let show = BottomOne;
    if (this.state.show === BottomOne) {
      show = BottomTwo;
    }

    this.setState({ show });
  }

  render() {
    return (
      <div>
        <button onClick={this.toggleComponent}>Change</button>
        <this.state.show />
      </div>
    );
  }
}

ReactDOM.render(<Example />, document.getElementById('root'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id="root"></div>

于 2017-09-05T19:16:27.153 回答
1

为了实现这一点,在父组件中维护一个状态变量(组件的某种标识符)并使用该状态变量来呈现不同的组件。

除此之外,您还需要将一个函数从父级传递给子级,并使用该函数来更新父级状态值。

像这样:

class App extends Component {
  constructor(){
    super();
    this.state={
      renderOne: true,
    }
    this.update = this.update.bind(this);
  }

  update(){
      this.setState({renderOne: false})
  }

  render() {
    return (
      <div className="App">
        <BodyContent update={this.update}/>
        {this.state.renderOne?  <BottomOne />  : <BottomTwo/> }
      </div>
    );
  }
}

现在在BodyContent组件调用内部this.props.update()渲染另一个组件。

于 2017-09-05T19:12:44.190 回答