Can you call an object from the onclick event?

0

I have a li object and I want to create an object from its onclick event. What I want is to be able to create an object from the event click of list item .

Can it be done? If you can not, is there an alternative?

Example:

class Ejemplo extends Component{

    liclick(event){
        <Objeto2 name="hola"/>
    }
    render(){
        return(
            <li onClick={this.liclick.bind(this)}>
        )
    }
}

class Objeto2 extends Component{

    constructor(props) {
        super(props);
    }
    render(){
        return(
            {this.props.name}
        )
    }
}
    
asked by sub 25.03.2017 в 21:42
source

1 answer

1

I answer myself. The only place where the objects are rendered is in the render method. What can be done is a state that is activated when you click with the button. As follows ...

class Ejemplo extends Component{

  constructor(props){
    super(props);
    this.state = {
    showComponent:false,
  };
 }

  liclick(event){
     this.setState({
       showComponent:true,
    });
  }
  render(){
    return(
        <div>
        <li onClick={this.liclick.bind(this)}>
        {this.state.showComponent ? <Chart symbol=   {this.props.stock.symbol}/> : null}
        <div>
    )
  }
}

class Objeto2 extends Component{

  constructor(props) {
    super(props);
  }
  render(){
    return(
        {this.props.name}
    )
  }
}
    
answered by 26.03.2017 в 18:20