Search code examples
javascriptreactjseventsjsdoc

How to document the "setState" function


Issue

I am trying to document events with JSdoc. Here is the example I followed:

/**
 * Throw a snowball.
 *
 * @fires Hurl#snowball
 */
Hurl.prototype.snowball = function() {
    /**
     * Snowball event.
     *
     * @event Hurl#snowball
     * @type {object}
     * @property {boolean} isPacked - Indicates whether the snowball is tightly packed.
     */
    this.emit('snowball', {
        isPacked: this._snowball.isPacked
    });
};    

Following that example, I documented my events as follows.

Inside a class named "Something"
/**
   Update state to include entered Thing Name
   @fires Something#handleThingChange
*/
  handleThingChange = (evt) => {
    /**
      handleThingChange event.

      @event Something#handleThingChange
      @type {updater}
      @property {string} state.thing
    */
    this.setState({ thing: evt.target.value });
  }

Question

I am unsure on how to document the setState function. Is it of type updater? Or does it warrant the @callback tag?


Solution

  • Based on your code i would say that it'll be of type updater.

    Explanation

    The ReactComponent.setState "updates" the current component state so it would be meaningful to document it as such. Yes the method has an optional callback parameter but i wouldn't take that into account because you don't use it.