1、组件

  1. //Tips:每一个页面级的组件必须加下面这行代码
  2. import React from 'react';

2、无状态组件(函数)

  1. import React from 'react';
  2. function App(){
  3. return {
  4. <div className="App">
  5. Hello world
  6. </div>
  7. }
  8. }

3、有状态组件

  1. import React from 'react';
  2. class App extends React.Component{
  3. constructor(props){
  4. super(props);
  5. this.state = {
  6. msg:"hello world"
  7. }
  8. }
  9. render(){
  10. return (
  11. <div>{this.state.msg}</div>
  12. )
  13. }
  14. }
  15. //jsx
  16. export default App;

4、事件

  1. import React from 'react';
  2. class App extends React.Component{
  3. constructor(props){
  4. super(props);
  5. this.state = {
  6. msg:"hello world"
  7. }
  8. }
  9. render(){
  10. return (
  11. <div onClick="{this.handleClick}">{this.state.msg}</div>
  12. )
  13. }
  14. handleClick(){
  15. console.log(1)
  16. }
  17. }
  18. //jsx
  19. export default App;

5、改变事件内部this关键字的指向 bind(this)

  1. import React from 'react';
  2. class App extends React.Component{
  3. constructor(props){
  4. super(props);
  5. this.state = {
  6. msg:"hello world"
  7. }
  8. }
  9. render(){
  10. return (
  11. //bind(this)改变this关键字的指向
  12. <div onClick={this.handleClick.bind(this)}>{this.state.msg}</div>
  13. )
  14. }
  15. handleClick(){
  16. this.setState({
  17. msg:"change"
  18. })
  19. }
  20. }
  21. //jsx
  22. export default App;
  1. render(){
  2. return (
  3. <div onClick={this.handleClick}>{this.state.msg}</div>
  4. )
  5. }
  6. handleClick=()=>{
  7. this.setState({
  8. msg:"change"
  9. })
  10. }

6、事件传参 bind(this,””)

  1. //Tips:传递参数一定要加bind bind(this,"")
  1. render(){
  2. return (
  3. <div onClick={this.handleClick.bind(this,"good")}>{this.state.msg}</div>
  4. )
  5. }
  6. handleClick=(val)=>{
  7. console.log(val)
  8. this.setState({
  9. msg:"change"
  10. })
  11. }

7、获取input值(获取键盘码)

  1. class App extends React.Component{
  2. constructor(props){
  3. super(props);
  4. this.state = {
  5. }
  6. }
  7. render(){
  8. return (
  9. <div>
  10. <input text="text" onKeyUp={this.handleKeyUp} />
  11. </div>
  12. )
  13. }
  14. handleKeyUp=(event)=>{
  15. console.log(event.keyCode)
  16. }
  17. componentDidMount() {
  18. console.log("1")
  19. }
  20. }

8、获取input输入框的值

  1. <input type="text" placeholder="添加ToDo" onChange={this.handleAdd.bind(this)}
  2. defaultValue={this.state.value} />
  1. constructor(props){
  2. super(props);
  3. this.state = {
  4. value:''
  5. }
  6. }
  1. handleAdd(event){
  2. console.log(event.target.value)
  3. this.setState({
  4. value:event.target.value
  5. })
  6. }

9、if-else

  1. class App extends React.Component{
  2. constructor(props){
  3. super(props);
  4. this.state = {
  5. isShow:true
  6. }
  7. }
  8. render(){
  9. return (
  10. <div>
  11. {this.Message()}
  12. </div>
  13. )
  14. }
  15. Message(){
  16. if(this.state.isShow){
  17. return (<span>显示</span>)
  18. }else{
  19. return (<span>隐藏</span>)
  20. }
  21. }
  22. }

10、三元

  1. class App extends React.Component{
  2. constructor(props){
  3. super(props);
  4. this.state = {
  5. isShow:true
  6. }
  7. }
  8. render(){
  9. return (
  10. <div>
  11. <p>{this.state.isShow?'good':'hello world'}</p>
  12. </div>
  13. </div>
  14. )
  15. }
  16. }

11、循环(for map)

  1. class App extends React.Component{
  2. constructor(props){
  3. super(props);
  4. this.state = {
  5. arr:[1,2,3]
  6. }
  7. }
  8. render(){
  9. return (
  10. <div>
  11. <div>{this.state.arr.map(item=>{
  12. return (<p>{item}</p>)
  13. })}
  14. </div>
  15. </div>
  16. )
  17. }
  18. }

12、input实现双向数据绑定

  1. //index.js
  2. import React , {Component} from 'react';
  3. import {Input} from 'antd';
  4. class App extends Component {
  5. constructor (props){
  6. super(props)
  7. this.state = {
  8. userName:"li"
  9. }
  10. }
  11. render() {
  12. return (
  13. <div>
  14. <Input type="text" value={this.state.userName} onChange={this.handleChange} />
  15. <p>{this.state.userName}</p>
  16. </div>
  17. )
  18. }
  19. handleChange=(e)=>{
  20. this.setState({
  21. userName:e.target.value
  22. })
  23. }
  24. }
  25. export default App;

13、内联样式

  1. <p style={{width:200,margin-top:"10px"}}>hello world</p>