0

I am using OpenLayer library and I need change values in ComponentDidMount() but when I am clicking it does not affect. I have used forceUpdate().

  handleDisable = () => {
  this.setState({ status: true });
  this.opacity_value = this.state.text;


  console.log(this.opacity_value);
  this.forceUpdate();
};

initMap = () => {


  console.log(this.opacity_value);
  wms_layer.setOpacity(this.opacity_value);

};
componentDidMount = () => {
  this.initMap();
};

render();
{
  return (
    <div>
      <Button
        content="Save"
        primary
        icon="save"
        onClick={() => this.handleDisable()}
      ></Button>
    </div>
  );
}
  • Your code is really bad. You must invoke layer methods inside the click handler, right now you just change these parameters in react component. Openlayers Map and all the objects within do not listen to any changes you make somewhere else. If you want to do something with the map 3 times, you invoke a proper method 3 times not once with a variable that is changed somewhere. – dmh126 Jul 18 at 7:20
  • I would like when clicking button it affects to componentDidMount values also. Is there any way? and do you ave any example about layer methods? – Elvin Jul 18 at 7:25
  • ComponentDidMount method is a component lifecycle method and it is triggered once mounting has been done. When component is mounted you use an event handler. If you want to set a different opacity on click, you need to invoke wms_layer.setOpacity(value) inside the handleDisable function. It’s all about the code structure with minimum react component lifecycle. – dmh126 Jul 18 at 7:39
  • Thanks a lot, it worked) – Elvin Jul 18 at 8:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.