javascript - switch components in react js -


i'm doing singlepage application , switch component.

here image how looks like:

image of spa

if click on button in component 3, switch component 3 5. maybe component 3 view of projects , if click on 1 project, see detail view of project information. created 2 different components this. other components should stay @ same place.

here code how switch components:

  this.state.detailvisible       ? <projectdetailview/>     : null 

i'm not sure if correct react way it. have 2 different css files component 3 , 5. if i'm switching 2 component, have class name irritations css.

if it's better way routers? how react way it?

thanks :)

it depends on needs, if need render both component3 , component5 route won't of help.
if need render 1 of them route can handy.
syntax prefer this:
this.state.detailvisible && <projectdetailview/>

here simple example:

const components = ["component1", "component2", "component3"];    class component extends react.component {    constructor(props) {      super(props);        this.state = {        showdetails: false      };        this.oncomponentclicked = this.oncomponentclicked.bind(this);    }    oncomponentclicked(e) {      this.setstate({        showdetails: !this.state.showdetails      });    }      render() {      const { name } = this.props;      const { showdetails } = this.state;      return (        <div>          <div>component{name}</div>          <button onclick={this.oncomponentclicked}>toggle details</button>          {showdetails && <componentdetails name={name} />}        </div>      );    }  }    const componentdetails = ({ name }) => (    <div>      <div>details of component{name}</div>    </div>  );    class app extends react.component {    render() {      return (        <div>          <h2>parent</h2>          {components.map(c => {            return (              <div>                <component name={c} />                <hr />              </div>            );          })}        </div>      );    }  }    reactdom.render(<app />, 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>


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -