Simon Breton Simon Breton - 1 year ago 99
React JSX Question

How do I use d3.nest inside my react-reducer?

I've the following reducer :

const initialState = {data1:null, data2:null, isFetching: false,error:null};
export const gaData = (state = initialState, action)=>{
switch (action.type) {
return Object.assign(
{data1: action.data1,
data2: ([x, y, z]) => ({ x, y, z }) ),
isFetching: action.isFetching,
error: null });
default:return state;

How do I use
so I can nest
value and rollup
value for

If I'm right I would to do something like this :

var newdata = d3.nest()
.key(function(d) { return d.x; })
.rollup(function(d) { return d3.sum(d, function(g) {return g.y;}); })

but I don't know do to include this in my reducer.

Answer Source

It sounds as though you just want to do something like this:

        let data2 =[x, y, z]) => ({ x, y, z }));
        data2 = d3.nest()
            .key(function (d) { return d.x; })
            .rollup(function (d) { return d3.sum(d, function (g) { return g.y; }); })

Then you can just return data2 as part of the new state object.

As an aside, consider changing your variable name to something more meaningful than data2!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download