• Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • About
  • Life
  • Tech
  • Travel
  • Work
  • Questions
  • Contact

Welcome

.

Binding a Bound Function in ReactJS Causing Problems

April 10, 2020 by

Questions › Binding a Bound Function in ReactJS Causing Problems
0
Vote Up
Vote Down
Garmaine asked 4 years ago

I am trying to make a list with an ability to add items to it and modify each item in the list.

The array it uses to make the list is a stateful variable(in this case subjects):

const [subjects, setSubjects] = useState([]);

To manipulate this array, I have a few functions:

function makeItem({ name, id, setActive, changeElement }) {
    let boundItemClick = setActive.bind(null, id);
    let changeName = changeElement.bind(null, id);
    return {
        name: name,
        id: id,
        onClick: boundItemClick,
        onChange: changeName
    };
}

function addItem( getID, setActive, containerArray, setContainerArray, changeElement) {
    setContainerArray([makeItem({ name: "", id: getID(), setActive: setActive, changeElement: changeElement }), ...containerArray]);
}

function getID(array){
    return array.length;
}

function changeContainerArray(containerArray, setContainerArray, id, e) {
    console.log(id);
    console.log(containerArray);
    containerArray[id].name = e.target.value;
    setContainerArray(containerArray);
}

To make an item, I pass it several components, but the one causing problems here is changeElement. changeElement should take the text present in an input box corresponding to the item, and update the item's name to match what is in the input box. The function changeElement should require the item id so that it knows which item to change, hence I bind it with the item's id and make that a property of item (which I later pass into an input field: onChange={item.onChange}).

Now, changeElement is itself a bound function that expects a few more components. It stems from the function changeContainerArray:

let changeSubjects = changeContainerArray.bind(null, subjects, setSubjects);
let getNewSubjectID = getID.bind(null, subjects);
let addSubject = addItem.bind(null, getNewSubjectID, setActiveSubject, subjects, setSubjects, changeSubjects);

Here, changeSubjects represents that array. I bind subjects and setSubjects to it, so that it can access and change them. It expects and id and the event, and it will lookup the array element with that id and change its name to the value of the input. Therefore, changeElement is this bound changeSubjects bound again with an item's id.

The problem is that, when I actually try to type something into an item's input(representing its name), the code crashes. I get TypeError: containerArray[id] is undefined, and, upon further inspection, it turns out that, when I log containerArray, it contains every element in subjects up to id-1. That is, it contains the array subjects at the time that changeElement is bound to id. Clearly, I don't fully understand binding and what problems it could cause, but how could I bind id to changeElement while still retaining subjects as a reference rather than a for some reason copied value?

Are you looking for the answer?
Original Question and Possible Answers can be found on `http://stackoverflow.com`

Question Tags: javascript, reactjs

Please login or Register to submit your answer




Primary Sidebar

Tags

Advancements best Business strategies commercial convenience economic Finances Cognitive decline Financial growth firm Future Hidden Gems Home hydration Impact Innovations lighting line of work Mental health Must-See New York City office patronage Productivity profession Profitability tips Profit optimization pursuit recreation Revenue enhancement romance sippy cups social station Technological breakthroughs technology toddlers trading transaction Treasures Uncover undertaking Well-being Wonders Work Young onset dementia

Newsletter

Complete the form below, and we'll send you all the latest news.

Footer

Footer Funnies

Who knew that reading the footer could be such a hilarious adventure? As we navigate websites, books, and documents, we often stumble upon the unassuming space at the bottom, only to discover a treasure trove of amusement. In this side-splitting compilation, we present 100 jokes that celebrate the unsung hero of content – the footer. Get ready to chuckle, giggle, and maybe even snort as we dive into the world of footnotes, disclaimers, and hidden comedic gems. Brace yourself for a wild ride through the footer!

Recent

  • Unveiling the Enigma: Almost-Magical Lamp Lights Highway Turns
  • The Impact of Young Onset Dementia on Employment and Finances: Optimizing Post-Diagnostic Approaches
  • 11 Wonders of 2023 Technological Breakthrough – Unveiling the Future
  • Work from Home and Stay Mentally Sane – Achieve Productivity and Well-being
  • Hidden Gems of New York City – Uncover the Must-See Treasures!

Search

Tags

Advancements best Business strategies commercial convenience economic Finances Cognitive decline Financial growth firm Future Hidden Gems Home hydration Impact Innovations lighting line of work Mental health Must-See New York City office patronage Productivity profession Profitability tips Profit optimization pursuit recreation Revenue enhancement romance sippy cups social station Technological breakthroughs technology toddlers trading transaction Treasures Uncover undertaking Well-being Wonders Work Young onset dementia

Copyright © 2023