I've been trying to solve this on my own for hours and am raising my hand now to see if anyone has any ideas.
The Problem
I'm making a dynamic sidebar filter that iterates over a groups
prop and returns a Link
with a query prop to update the router. There's a checkbox displayed and is flagged based on if that group id is currently excluded.
The Code
render: function() {
return (
<ul>
{this.props.query.filter == 'connections' ?
// the array of groups to build the filter for
this.props.groups.map(function (group) {
// this array would start with the currently excluded ids, and end with the new list of ids
var new_ids = this.context.router.getCurrentQuery().exclude || [];
var index = new_ids.indexOf(group.id.toString());
console.log('my id: ' + group.id);
console.log('starting: ' + new_ids);
// again, the array is excluded ids, so if it's not in the array it should be checked
var selected = index == -1;
if (selected) {
// if this link is clicked, add the id to the excludes
new_ids.push(group.id);
}
else {
// if this linked is clicked, remove the id from the excludes
new_ids.splice(index, 1);
}
console.log('ending: '+new_ids);
// return the preloaded link
return <Link key={group.name}
to="feed"
query={{filter: 'connections', exclude: new_ids}}>
<small>{group.name}</small>
</Link>;
}, this) : null }
</ul>
);
This worked when I was using input type="checkbox"
and an event handler with this.transitionTo
, but I wanted to use the Link
components to handle the requests instead of an event handler.
The Results
The page works fine for the first click (query.exclude == undefined
) but after that the new_ids
is mutating with each iteration.
This is the console output...
id: 11
starting:
new ids: 11
id: 6
starting:
new ids: 6
id: 21
starting:
new ids: 21
After you click one (say the first group -- id 11, it messes up)...
id: 11
starting: 11
new ids: // this is correct, it removes 11 from the list
id: 6
starting: // this should be 11, instead its inheriting the prior value
new ids: 6 // this should be 11, 6
id: 21
starting: 6 // this should be 11, instead its inheriting the prior value
new ids: 6,21 // this should be 11, 21
I've tried making this iteration a for ... loop
instead of the .map()
but that didn't help. I've also moved the initial excluded_ids out of the iteration, but again same result.
Again, all this is supposed to do is generate the values of the query.exclude
prop for the navigation based on the result of clicking the link.
Any ideas as to what could be up would be appreciated. Thank you.