0

I'm using state to open and close a layer in a click event of an icon. When I call the _onOpenLayer() from the click handler nothing happens.

Previously I had called the method direct from the icon click onClick={this._onOpenLayer()}, this did open the layer but if froze up the UI due to not being allowed to call a method within rednder().

So I found a solution of adding a lambda before the call to open as suggested below. But clicking the icon doesn't open the layer with this change:

onClick={() => this._onOpenLayer()} 

To further debug this I put a console.log in the _onOpenLayer() method and I can see it doesn't get hit on the icon click.

Question:

How can you call a method from click event within render method?

This is a gist of the class I'm rendering. The _onOpenLayer() sets the openLayer state to true which in turn opens the layer element:

class Page1 extends Component {
  constructor(props) {
    super(props);
    this.state = {
        openLayer: false,
    };    
  }


    _onOpenLayer() {

        console.log("fooo");
        this.setState({
            openLayer: true
        });
    }


    _onCloseLayer() {
    this.setState({
        openLayer: false
        });

    }


    render() {
    let layerNode;
    if (this.state.openLayer) {
      layerNode = (
        <Layer flush={true} closer={true} onClose={() => this._onCloseLayer()} align='right'>
        </Layer>
      );
    }    


    return (
      <Section pad="small" full="vertical" flex={true}>
            <div>
                <Box direction="row" align="center" justify="end"  tag="aside" pad={{horizontal: "large" , between: "small"}} >
                    <Filter size="medium" colorIndex="brand" onClick={() => this._onOpenLayer()} />
                </Box>
                {layerNode}

      </Section>
    );
  }
};
Community
  • 1
  • 1
Brian Var
  • 6,029
  • 25
  • 114
  • 212

3 Answers3

2

Ensure the icon is clickable(Check the pointer-events in css) and ensure onClick is triggered.

Else try this....

class Page1 extends Component {
  constructor(props) {
    super(props);
    this.state = {
        openLayer: false,
    };
    this._onOpenLayer = this._onOpenLayer.bind(this)
    this._onCloseLayer = this._onCloseLayer.bind(this)
  }


    _onOpenLayer() {

        console.log("fooo");
        this.setState({
            openLayer: true
        });
    }


    _onCloseLayer() {
    this.setState({
        openLayer: false
        });

    }


    render() {
    let layerNode;
    if (this.state.openLayer) {
      layerNode = (
        <Layer flush={true} closer={true} onClose={this._onCloseLayer} align='right'>
        </Layer>
      );
    }    


    return (
      <Section pad="small" full="vertical" flex={true}>
            <div>
                <Box direction="row" align="center" justify="end"  tag="aside" pad={{horizontal: "large" , between: "small"}} >
                    <Filter size="medium" colorIndex="brand" onClick={this._onOpenLayer} />
                </Box>
                {layerNode}

      </Section>
    );
  }
};
Pranesh Ravi
  • 18,642
  • 9
  • 46
  • 70
1

I assume that you want to call _onOpenLayer() method not in render() but on every icon click.

So instead of calling that method in render:

onClick={this._onOpenLayer()}

you can just pass your function to onClick prop

onClick={this._onOpenLayer.bind(this)}

0

The lambda is used by es2015 / es6 and you need to use polyfill or babel...

class Page1 extends Component {
  constructor(props) {
    super(props);
    this.state = {
        openLayer: false,
    };    
  }

    _onOpenLayer() {

        console.log("fooo");
        this.setState({
            openLayer: true
        });
    }


    _onCloseLayer() {
    this.setState({
        openLayer: false
        });

    }


    render() {
    let layerNode;
    if (this.state.openLayer) {
      layerNode = (
        <Layer flush={true} closer={true} onClose={this._onCloseLayer} align='right'>
        </Layer>
      );
    }    


    return (
      <Section pad="small" full="vertical" flex={true}>
            <div>
                <Box direction="row" align="center" justify="end"  tag="aside" pad={{horizontal: "large" , between: "small"}} >
                    <Filter size="medium" colorIndex="brand" onClick={this._onOpenLayer} />
                </Box>
                {layerNode}

      </Section>
    );
  }
};
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
hod caspi
  • 826
  • 2
  • 10
  • 17