https://www.tutorialspoint.com/reactjs/index.htm
- https://medium.com/@robhitt/react-from-scratch-575d1e570b85
- http://manojsinghnegi.com/blog/2017/09/03/Implementing-redux-and-react-router-v4-in-your-react-app/
https://blog.risingstack.com/using-react-with-webpack-tutorial/
- https://css-tricks.com/css-modules-part-3-react/
- https://survivejs.com/react/advanced-techniques/styling-react/
- https://codeburst.io/4-four-ways-to-style-react-components-ac6f323da822
"if you have objects inside arrays or arrays inside objects and want them too to be immutable, your choice is Immutable.fromJS"
http://2ality.com/2014/09/es6-modules-final.html There are two kinds of exports: named exports (several per module) and default exports (one per module).
https://medium.com/@reasoncode/javascript-es6-arrow-functions-and-lexical-this-f2a3e2a5e8c4
const foo = function(a){
return a;
}
// Arrow function
// - Also bind "this"
const foo = (a) => {
return a;
}
const foo = (a) => (a);
const Todo = ({ onClick, completed, text }) => (
<li
onClick={onClick}
style={ {
textDecoration: completed ? 'line-through' : 'none'
}}
>
{text}
</li>
)
same as
const Todo = (props) => (
<li
onClick={props.onClick}
style={ {
textDecoration: props.completed ? 'line-through' : 'none'
}}
>
{props.text}
</li>
)
- setState() method is used to update the state of the component. This method will not replace the state, but only add changes to the original state.
Whenever this.setState
is called, an update to the component is scheduled, causing React to merge in the passed state update and rerender the component along with its descendants.
-
NEVER mutate
this.state
directly, as callingsetState()
afterwards may replace the mutation you made. Treat this.state as if it were immutable. -
setState()
does not immediately mutate this.state but creates a pending state transition. Accessingthis.state
after calling this method can potentially return the existing value. -
There is no guarantee of synchronous operation of calls to setState and calls may be batched for performance gains.
setState()
will always trigger a re-render unless conditional rendering logic is implemented inshouldComponentUpdate()
. -
If mutable objects are being used and the logic cannot be implemented in
shouldComponentUpdate()
, callingsetState()
only when the new state differs from the previous state will avoid unnecessary re-renders.
functional components
Rather than define a class extending React.Component, simply write a function that takes props and returns what should be rendered.
- https://www.tutorialspoint.com/reactjs/reactjs_component_life_cycle.htm
- https://reactjs.org/docs/react-component.html
https://stackoverflow.com/questions/41768205/difference-between-import-react-and-import-component-syntax
https://stackoverflow.com/questions/33956201/how-to-import-and-export-components-using-react-es6-webpack
Default import. Default imports are exprted with export default ...
. There can be only a single default export.
import React from 'react'
Member import (named import). Member imports are exported with export ...
. There can be many member exports.
import { Component } from 'react'
You can import both by using this synctax:
import React, { Component } from 'react';
To export a single component in ES6, you can use export default
as follows:
class MyClass extends Component {
...
}
export default MyClass;
And now you can use the following syntax to import that module:
import MyClass from './MyClass.react'
if you are looking to export multiple components from a single file the declaration would look something like this:
export class MyClass extends Component {
...
}
export Class MyClass extends Component {
...
}
And now you can use the following syntax to import those files:
import {MyClass1, MyClass2} from './MyClass.react'
#########################################################################
- https://www.tutorialspoint.com/reactjs/reactjs_props_validation.htm App.propTypes is used for props validation. If some of the props aren't using the correct type that we assigned, we will get a console warning. After we specify validation patterns, we will set App.defaultProps.
import PropTypes from 'prop-types';
import React from 'react';
import ReactDOM from 'react-dom';
class App extends React.Component {
render() {
return (
<div>
<h1> Hello, {this.props.name} </h1>
<h3>Array: {this.props.propArray}</h3>
<h3>Bool: {this.props.propBool ? "True..." : "False..."}</h3>
<h3>Func: {this.props.propFunc(3)}</h3>
<h3>Number: {this.props.propNumber}</h3>
<h3>String: {this.props.propString}</h3>
</div>
);
}
}
App.propTypes = {
name: PropTypes.string,
propArray: PropTypes.array.isRequired,
propBool: PropTypes.bool.isRequired,
propFunc: PropTypes.func,
propNumber: PropTypes.number,
propString: PropTypes.string,
};
App.defaultProps = {
name: 'Tutorialspoint.com',
propArray: [1, 2, 3, 4, 5],
propBool: true,
propFunc: function(e) {
return e
},
propNumber: 1,
propString: "String value..."
}
we have use isRequired when validating propArray and propBool. This will give us an error, if one of those two don't exist. If we delete propArray: [1,2,3,4,5] from the App.defaultProps object, the console will log a warning.
If we set the value of propArray: 1, React will warn us that the propType validation has failed, since we need an array and we got a number. ###################################################################
- The main difference between state and props is that props are immutable. This is why the container component should define the state that can be updated and changed, while the child components should only pass data from the state using props.
class App extends React.Component {
...
}
App.defaultProps = {
headerProp: "Header from props...",
contentProp:"Content from props..."
}
- JS use 2 spaces instead of tab
- It is, however, conventional in React apps to use on* names for the attributes and handle* for the handler methods.
docker container exec -it dev_env bash