G. Cox
G. Cox

Reputation: 119

How to rewrite this double arrow function

I have a function that updates my the state of schedule, an array of artist objects. Currently I am using a double arrow function that takes in the index and the artist id. However I can't use a double arrow function due to my javascript linter. How can I rewrite this?

handleArtistChange = index => evt => {
        if (evt) {
            const newSchedule = this.state.schedule.map((artist, stateIndex) => {
                if (index !== stateIndex) return artist;
                return { ...artist, artist_id: evt.value };
            });
            this.setState({ schedule: newSchedule });
        }
}

I have tried the following:

handleArtistChange = function(index) {
        return function(evt) {
            if (evt) {
                const newSchedule = this.state.schedule.map((artist, stateIndex) => {
                    if (index !== stateIndex) return artist;
                    return { ...artist, artist_id: evt.value };
                });
                this.setState({ schedule: newSchedule });
            }
        }
    }

However this results in an error of Cannot read property 'schedule' of undefined

The call to my function:

const lineup = this.state.schedule.map((artist, index) => {
            return (
                <div key={index} className="form__input-group--lineup">
                    <div>
                        <label className="form__label">{getTextNode('Artist *')}</label>
                        <Select
                            onChange={this.handleArtistChange(index)}
                            onInputChange={this.handleInputChange}
                            isClearable
                            options={options}
                            styles={customStyles}
                            backspaceRemovesValue={false}
                            placeholder={`Artist #${index + 1}`}
                            classNamePrefix="react-select"
                        />
                    </div>
                    <div className="form__input-group--time">
                        <label className="form__label">{getTextNode('start time *')}</label>
                        <input name="startTime" type="time" required autoComplete="true" className="form__input" value={this.state.startTime} onChange={this.handleTimeChange(index)} />
                    </div>
                    <button type="button">-</button>
                </div>
            );
        });

Upvotes: 1

Views: 105

Answers (1)

jo_va
jo_va

Reputation: 13963

You could modify your linting rules if necessary. If you want to modify your function, here is a way to define it, with a regular function returning an anonymous function bound to the outer this:

function handleArtistChange(index) {
    return (function(evt) {
        if (evt) {
            const newSchedule = this.state.schedule.map((artist, stateIndex) => {
                if (index !== stateIndex) return artist;
                return { ...artist, artist_id: evt.value };
            });
            this.setState({ schedule: newSchedule });
        }
    }).bind(this);
}

Upvotes: 1

Related Questions