ruanyf /react-demos
a collection of simple demos of React.js
★ 16,413⑂ 6,227👁 1,046⑃ 69● 34▤ 0
More from this author
+0
Daily
16,413
stars
6,227
forks
1,046
watchers
69
prs
34
issues
0
releases
README
This is a collection of simple demos of React.js. These demos are purposely written in a simple and clear style. You will find no difficulty in following them to learn the powerful library. Related Projects - Flux Demo - Webpack Demos - React Router Tutorial - CSS Modules Demos - React Testing Demo - A boilerplate for React-Babel-Webpack project How to use First, copy the repo into your disk. bash $ git clone [email protected]:ruanyf/react-demos.git Then play with the source files under the repo's demo directories. HTML Template html // Our code goes here! Index 1. Render JSX 1. Use JavaScript in JSX 1. Use array in JSX 1. Define a component 1. this.props.children 1. PropTypes 1. Finding a DOM node 1. this.state 1. Form 1. Component Lifecycle 1. Ajax 1. Display value from a Promise 1. Server-side rendering --- Demo01: Render JSX demo / source The template syntax in React is called JSX. JSX allows you to use HTML tags in JavaScript code. ReactDOM.render() is the method which translates JSX into HTML and renders it into the specified DOM node. js ReactDOM.render( Hello, world!, document.getElementById('example') ); To actually perform the transformation in the browser, you must use to indicate JSX code, and include babel.min.js, which is a browser version of Babel and can be found in the babel-core@6 npm release. Before v0.14, React used JSTransform.js to translate , but this is now deprecated (more info). Demo02: Use JavaScript in JSX demo / source You can also use JavaScript within JSX. Angle brackets (<) symbolize the beginning of HTML syntax, while curly brackets ({) represent the beginning of JavaScript syntax. js var names = ['Alice', 'Emily', 'Kate']; ReactDOM.render( { names.map(function (name) { return Hello, {name}! }) } , document.getElementById('example') ); Demo03: Use array in JSX demo / source If a JavaScript variable is an array, JSX will implicitly concat all members of the array. js var arr = [ Hello world!, React is awesome, ]; ReactDOM.render( {arr}, document.getElementById('example') ); Demo04: Define a component demo / source class ComponentName extends React.Component creates a component class, which implements a render method to return a component instance of the class. Before v16.0, React used React.createClass() to create a component class, but this is now deprecated (more info). javascript class HelloMessage extends React.Component { render() { return Hello {this.props.name}; } } ReactDOM.render( , document.getElementById('example') ); You can use this.props.[attribute] to access the attributes of a component. Example: this.props.name of is John. Please remember the first letter of the component's name must be capitalized, otherwise, React will throw an error. For instance, HelloMessage as a component's name is OK, but helloMessage is not allowed. And a React com