Choropleth 2 questions

Hello, I finished the choropleth project with all tests passing but with 2 weird lay-out things that bug me a little bit.
link:here

1)first, my map doesn’t show at the top of the page as one would expect but much lower on the page, I have looked at it but can see no reason why

2)even more weird, my “legend” gets displayed twice, on above the map, once underneath it.
again, I have no clue why that would be.

Probably I am not the first one to get this so I’m hoping someone can help me.
Thanks in advance.

Hello there,

Just a quick look, I would guess this is classic React rendering multiple times, causing the odd output. React does not benefit this use case…at all, from what I can see.

Also, it is quite odd having async methods, and binding them outside of the constructor:

async getEducationData(){
    let response=await fetch(educationUrl);
    let json= await response.json();
    this.setState({
      educationData:json
    });
    this.getCountyData=this.getCountyData.bind(this);
    this.getEducationData=this.getEducationData.bind(this)
  }

Originally, I would not have expected any output to show, because of the logic in the render method relying on the return of itself. Again, React is not leveraged in this project.

Otherwise, stick some console.logs throughout the sections to see what is run when.

Hope this helps

1 Like

Hi and thanks for your time,
I have done all my data visualization projects with React including the async methods, so It’s normal those work and that was not really the issue I think.
The point wass not to learn to do the project without React, but to do the project and at the same time learn a little bit more about React, for example knowing why does the legend display twice for no reason.
What exactly do you mean with “React is not leveraged in this project”?
Should I use it differently or not at all? And what if I wanted/needed to use d3 with front-end framework?

I am glad you are taking the opportunity to learn and practice these skills. It is just, currently, the app basically contains React boilerplate, with HTML…no JSX which is a main feature of React.

A major reason a developer might choose to use React is to help reduce the amount of code they need to write. This is mainly done by:

  1. Creating and reusing multiple components. Create once, use many times.
  2. Throw a lot of JSX into your components:
// This is a silly example. The fCC React course covers common use cases
<>
  {aBunchOfStuffIDoNotWantToHardCode.map(stuff => {
    return (<>{stuff.moreStuff}</>)}
</>

Another main feature of React is state. In the app, the only state is the data the app fetches once upon mount. React is designed to work around state by rendering/re-rendering based on state changes. Your app’s state never really changes. So, you might as well not use it.

As this project stands, I would say not at all. It is why I specifically did not use React when I did it. You are introduced to the idiosyncrasies of React (eg. It tends to re-render frequently), and it is useful to know this, but all it is doing, in this project, is causing bugs and confusion.

Remember, another main technology you are taught is AJAX. This is what is expected of most/all of the Data Visualisation Projects.


All that said, please feel free to keep using React in this project. I would just argue that there is not much scope for campers to be learning it well within this section of the course. I cannot think of a way to leverage React here.

Hope this clarifies.

2 Likes

Hi @sdbondt. I am afraid @Sky020 is right. Knowing when to use react and when not to use it is also very important. Your code for creating the map is in the render method. Essentially your component, I believe, is rendered twice. On mount and after fetching the data and updating state. If you check in the console there are four svgs.
The first two are created on mount and the last two after updating state and re-rendering the component. The reason why your map is displaced down is because of the first two svgs. First with a class of svg followed by another with id of legend.

<svg width="1000" height="800" class="svg"></svg>
<svg id="legend" width="330" height="50"><rect height="20" width="33.333333333333336" y="0" x="0" style="fill: rgb(247, 252, 245);"></rect><rect height="20" width="33.333333333333336" y="0" x="33.333333333333336" style="fill: rgb(229, 245, 224);"></rect><rect height="20" width="33.333333333333336" y="0" x="66.66666666666667" style="fill: rgb(199, 233, 192);"></rect><rect height="20" width="33.333333333333336" y="0" x="100" style="fill: rgb(161, 217, 155);"></rect><rect height="20" width="33.333333333333336" y="0" x="133.33333333333334" style="fill: rgb(116, 196, 118);"></rect><rect height="20" width="33.333333333333336" y="0" x="166.66666666666669" style="fill: rgb(65, 171, 93);"></rect><rect height="20" width="33.333333333333336" y="0" x="200" style="fill: rgb(35, 139, 69);"></rect><rect height="20" width="33.333333333333336" y="0" x="233.33333333333334" style="fill: rgb(0, 109, 44);"></rect><rect height="20" width="33.333333333333336" y="0" x="266.6666666666667" style="fill: rgb(0, 68, 27);"></rect><g transform="translate(0,20)" fill="none" font-size="10" font-family="sans-serif" text-anchor="middle"><path class="domain" stroke="currentColor" d="M0.5,-20V0.5H300.5V-20"></path><g class="tick" opacity="1" transform="translate(0.5,0)"><line stroke="currentColor" y2="-20"></line><text fill="currentColor" y="3" dy="0.71em"></text></g><g class="tick" opacity="1" transform="translate(34.01724137931034,0)"><line stroke="currentColor" y2="-20"></line><text fill="currentColor" y="3" dy="0.71em">11%</text></g><g class="tick" opacity="1" transform="translate(67.53448275862068,0)"><line stroke="currentColor" y2="-20"></line><text fill="currentColor" y="3" dy="0.71em">19%</text></g><g class="tick" opacity="1" transform="translate(101.05172413793103,0)"><line stroke="currentColor" y2="-20"></line><text fill="currentColor" y="3" dy="0.71em">27%</text></g><g class="tick" opacity="1" transform="translate(134.56896551724137,0)"><line stroke="currentColor" y2="-20"></line><text fill="currentColor" y="3" dy="0.71em">35%</text></g><g class="tick" opacity="1" transform="translate(168.08620689655172,0)"><line stroke="currentColor" y2="-20"></line><text fill="currentColor" y="3" dy="0.71em">43%</text></g><g class="tick" opacity="1" transform="translate(201.60344827586206,0)"><line stroke="currentColor" y2="-20"></line><text fill="currentColor" y="3" dy="0.71em">51%</text></g><g class="tick" opacity="1" transform="translate(235.1206896551724,0)"><line stroke="currentColor" y2="-20"></line><text fill="currentColor" y="3" dy="0.71em">59%</text></g><g class="tick" opacity="1" transform="translate(268.63793103448273,0)"><line stroke="currentColor" y2="-20"></line><text fill="currentColor" y="3" dy="0.71em">67%</text></g></g></svg>

I assume the svgs you need are the last two. The ones rendered after the data has been fetched and state update. If you want to remove the first two svgs, add the following line of code after destructuring this.state so that you don’t execute the code which creates the map on mount before fetching the data.

if(educationData.length === 0){
      return null;
 }
1 Like

Thank you both for your time.
The reason I wanted to do this with React, was to learn more about that aswell, since your last suggestion solved both my problems, I did just that and will be able to do this on my own a next time.

Also, it’s good to hear I have not been using React 100% correctly, since nobody else was doing it this way, I had to come up with myself.