A playground repository that will be filled with educational resources and live code as our dev group (Angular experts) learns React/Redux by contrasting it to Angular concepts. This repository is best consumed by those that have been working with Angular 1.x for a good amount of time.
Similar to Yeoman's Yo Angular, the React/Redux community offers a starter kit that has everything you need. The code is rather intimdating at first, but don't worry as it will make sense as you continue to study.
Be sure to provide your editor with plugins for eslint, JavaScript ES6 syntax and React JSX syntax.
<div className="cards">
<div className="info">
{['x', 'y', 'z'].map((info) => <p>{info}</p>)}
</div>
</div>
Note that you can get the index by specifying an index parameter: ['x', 'y', 'z'].map((key, index) =>
.
In React, a variable that holds CSS values can be created and later assigned as the inline style of an element. Keep in mind that camelCase is used, so "background-color" becomes "backgroundColor", "font-size" becomes "fontSize", and so on.
If you want to alter the style of elements through a click event, similar to what is shown in the Angular documentation here, you can attach a function to the "onClick" React Synthetic Event. This function can then for instance result in the changing of a class name, or the direct manipulation of a variable that is serving as the value for a CSS property like "marginBottom", "padding", etc. In Example 2 and 3 this will be demonstrated with a variable that exists in local React State (not to be confused with Redux State).
class ngStyleExample1 extends React.Component {
constructor(props) {
super(props);
}
render() {
var firstExampleStyle = {backgroundColor: "green"};
return (
<div>
<p
id="firstExampleWithNoEventHandler"
style={firstExampleStyle}
>
Example 1 Blah Blah Blah
</p>
</div>
);
}
}
class ngStyleExample2and3 extends React.Component {
constructor(props) {
super(props);
this.state = {
cssClass : "show"
styleValue : "20"
};
}
/*
Second example
Clicking on the <p> tag changes its class, which alters its style
*/
handleClassClick() {
if(this.state.cssClass === "show") {
this.setState(cssClass : "hide");
} else {
this.setState(cssClass : "show");
}
}
/*
Third example
Clicking on the <p> tag directly alters the value on which the style is calculated (fontSize)
*/
handleStyleClick() {
this.setState(styleValue : this.state.styleValue + 5);
}
render() {
return (
<div>
<p
id="changeClassExample"
className={this.state.cssClass}
onClick = {() => this.handleClassClick}
>
Example 1 Blah Blah Blah
</p>
<p
id="directlyChangeStyleExample"
style={{fontSize : this.state.styleValue}}
onClick = { () => this.handleStyleClick
>
Example 2 Blah Blah Blah
</p>
</div>
);
}
}
Q: Wait... surely someone has researched and documented this (e.g.: SitePoint: React for Angular Developers), why reinvent the wheel?
A: Because first-person studies and writing code are the best approaches to learning.
MIT