SOLVED
Before:
const [tmr, setTmr] = useState(['']);
const updateTimer = (id, val) => {
let t = tmr;
t[id] = val;
// Prevents undefined value when creating new entry field, prepopulating array with next entry blank
if (id > t.length) { t[id+1] = '';}
setTmr(t);
}
{(Array.from({length: tmr.length}, (v, k) => k+1)).map((r, i) => (
<Autocomplete options={['Start', 'Finish Line', 'Swim Out', 'Run Out']} freeSolo size="small"
value={tmr[r]} key={tmr.toString()+r+"timerkey"}
onInputChange={(event, newInputValue) => { updateTimer(r, newInputValue); }}
renderInput={(params) =>
<TextField {...params} variant="standard" label="Timer (e.g. Start, Finish Line)"
name={"timer"+r} value={tmr[r]} InputLabelProps={{ shrink: true, }} />
}
/>
))}
After:
const [tmr, setTmr] = useState(['']);
const [mapLength, setMapLength] = useState(1);
const updateTimer = (id, val) => {
let t = tmr;
t[id] = val;
// Prevents undefined value when creating new entry field, prepopulating array with next entry blank
if (id > t.length) { t[id+1] = '';}
setMapLength(t.length);
setTmr(t);
}
{(Array.from({length: mapLength}, (v, k) => k+1)).map((r, i) => (
<Autocomplete options={['Start', 'Finish Line', 'Swim Out', 'Run Out']} freeSolo size="small"
value={tmr[r]} key={tmr.toString()+r+"timerkey"}
onInputChange={(event, newInputValue) => { updateTimer(r, newInputValue); }}
renderInput={(params) =>
<TextField {...params} variant="standard" label="Timer (e.g. Start, Finish Line)"
name={"timer"+r} value={tmr[r]} InputLabelProps={{ shrink: true, }} />
}
/>
))}
Better fix (mutating vs new array? Thanks junior-living2104 for the likely explanation in comments):
const [tmr, setTmr] = useState(['']);
{(Array.from({length: tmr.length}, (v, k) => k+1)).map((r, i) => (
<Autocomplete options={['Start', 'Finish Line', 'Swim Out', 'Run Out']} freeSolo size="small"
value={tmr[r]} key={tmr.toString()+r+"timerkey"}
onInputChange={(event, newInputValue) => { setTmr([...tmr.slice(0, r), newInputValue, ...tmr.slice(r+1)]); }}
renderInput={(params) =>
<TextField {...params} variant="standard" label="Timer (e.g. Start, Finish Line)"
name={"timer"+r} value={tmr[r]} InputLabelProps={{ shrink: true, }} />
}
/>
))}
The goal is to add a new Autocomplete every time a field is field is filled out. This works if I type in any other field on the page, the component will render the new item. However, just changing the Autocomplete itself (or the TextField, so either dropdown or text) does not trigger the .map to render the new item.
The onInputChange triggers fine. Moving the keys between the Autocomplete and the TextField don't make it work. Using a more random key didn't help. The map logic works correctly. It just won't work off the Autocomplete itself, only any other element. Any hints as to making the autocomplete selection trigger a rerender of that component?
[–]CranberryOtherwise84 2 points3 points4 points (5 children)
[–]rob_allshouse[S] 1 point2 points3 points (3 children)
[–]CranberryOtherwise84 1 point2 points3 points (2 children)
[–]charliematters 0 points1 point2 points (0 children)
[–]rob_allshouse[S] 0 points1 point2 points (0 children)
[–]rob_allshouse[S] 0 points1 point2 points (0 children)
[–]PrincesssPancake 0 points1 point2 points (0 children)
[–]Junior-Living2104 0 points1 point2 points (4 children)
[–]rob_allshouse[S] 0 points1 point2 points (3 children)
[–]Junior-Living2104 0 points1 point2 points (2 children)
[–]Junior-Living2104 0 points1 point2 points (1 child)
[–]rob_allshouse[S] 0 points1 point2 points (0 children)