Going from Data to UI in React


#1
Happening place to discuss this tutorial: http://www.kirupa.com/react/from_data_to_ui_in_react.htm

#2

Thank you for this wonderful article! There seems to be a mistake though I believe. When we add a key to each JSX component we push to the array, it should say:

 for (var i = 0; i < colors.length; i++) {
   renderData.push(<Circle key={i + colors[i]} bgColor={colors[i]}/>);
}

Otherwise, only a randomised circle will be displayed.


#3

Dridge - the snippet I posted towards the end does something very similar:

for (var i = 0; i < colors.length; i++) {
  var color = colors[i];
  renderData.push(<Circle key={i + color} bgColor={color}/>);
}

I just created a color variable that stores the value of colors[i]. The end result between our code should be the same, right? Or am I overlooking something? It’s been one of those days :stuck_out_tongue:

Cheers,
Kirupa


#4

:joy: I totally overlooked the statement “var color = colors[i];”. Sorry and thanks again! :slight_smile:


#5

Hello,
I followed your step one by one as you did here, but I need to copy the background color of each button either to text color or background color of another component which is not a parent. I have create two rows of buttons; one to be used for text color and the other for the background color. How to use these background color from the parent to send them to other child. Hopefully you write the whole code. Thank you.


#6

I am having difficulty visualizing what you are describing. Do you have a diagram that illustrates the arrangement of the components and what values you are trying to transfer to which destination? :slight_smile:


#7

Yes, let’s say that the diagram like the following:

                           A
                         /   \
                        B     C
                      /   \
                     D     E

The D and E are the components that each include a class of creating a button and a list of color to style the background of the button, as you did here. B is a parent and instead of creating the next component as you did, I immediately placed the result of the list inside it. So I need to transfer the background color from either D or E to C. I have created a constructor in A to have a connection between B and C, but it didn’t work.I am beginner and need your help. Thank you.


#8

Ah! That is exactly the kind of scenario Redux can help you with. Take a look at the Redux Introduction and then the article on how to integrate Redux into your React App :slight_smile:


#9

And if redux seems overwhelming,

I have created a constructor in A to have a connection between B and C, but it didn’t work.

Seems like you were on the right track. You just need to manage everything through A, the common parent of them all. Here’s an example:


#10

A post was split to a new topic: React-redux error: Failed to execute ‘removeChild’ on ‘Node’


#11

Morning,

I am attempting to get this set of circles to work, but am getting the error:

ReferenceError: renderData is not defined[Learn More]

in my console. I’ve tried to move the renderData variable to multiple locations, but it doesn’t seem to resolve the issue. Currently I have it just below var circleStyle & var colors in the “render(){” section of the the code. See below:

makingCirclesReact (1.4 KB)

If you have some time to point me in the right direction it would be appreciated. I’m sure I have the var defined in the wrong spot I just don’t know how to fix it.

Thanks :slight_smile:


#12

Hi MountainTrailRover! You were really close. The code for render data and the array for populating it needs to live outside of the Circle component. Here is what the code inside the script tag should look like:

class Circle extends React.Component {
  render() {
    var circleStyle = {
      padding: 10,
      margin: 20,
      display: "inline-block",
      backgroundColor: this.props.bgColor,
      borderRadius: "50%",
      width: 100,
      height: 100
    };
    return (
      <div style={circleStyle}>
      </div>
    );
  }
}

var colors = ["#393E41", "#E94F37", "#1C89BF", "#A1D363", "#85FFC7", "#297373", "#FF8552", "#A40E4C"];

var renderData = [];

for (var i = 0; i < colors.length; i++) {
  var color = colors[i];
  renderData.push(<Circle key={i + color} bgColor={color}/>);
};

ReactDOM.render(
    <div>
    {renderData}
    </div>,
    document.querySelector("#container")
  );

This should do the trick…hopefully! If it doesn’t work, please reply back and we can look at some other solutions.

Cheers,
Kirupa :smile:


#13

Morning!

That worked beautifully, thanks for taking some time to reply. So the other variable for component Circle lives inside that component. Does the array get defined outside of it so you are able call multiple components?

Thanks again!

Cheers,
James


#14

Are you referring to the circleStyle variable? If so, that one is localized just for that component. It is designed to specify how our circle looks. You can totally put that outside of the component as well to have other components use it if you want.

The reason the colors, renderData, and for loop are defined outside is that we need to populate renderData for rendering inside ReactDOM.render. If we defined all of them inside the Circle component, you can’t easily access them outside of it.

I explain a bit more about all of this in the following tutorial: Variable Scope Basics in JavaScript.