0

I wants to create a class which while load a react.js file into a div specified using the react i had not found any thing related to that on net.

1
  • 1
    Mate, you need to provide more details, what you are asking is very ambiguous. Commented Feb 16, 2017 at 8:41

1 Answer 1

1

Check this example:

Lets say you have 2 component App and Child, and wants to render child component on checking the checkbox, this is called conditional rendering.

app.js file, import file child.js in App component:

import Child from './child';

class App extends React.Component {
    constructor() {
        super();
        this.showComments = this.showComments.bind(this);
        this.state = {
            showComponent: false,
        };
    }

    showComments(e) {
        this.setState({
            showComponent: e.target.checked,
        });
    }

    render() {
           return (
                <div className="add_checkbox">
                   <span>Enable Comments</span>
                   <input className="checkbox" type="checkbox" name="enable_comment" onClick={this.showComments} value="enable_comment"/>
                   {this.state.showComponent ? <Child/>  : null}
                </div>
        )
    }
}

child.js file:

export default class Child extends React.Component{
   render(){
       return(
          <div>Hello</div>
       );
   }
}

ReactDOM.render(<App />, document.getElementById('container'));

Check fiddle for working example: https://jsfiddle.net/ztx9kd1w/

Let me know if you need any help.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.