1. 程式人生 > >react基礎總結篇1,定義組件實現父子組件傳值

react基礎總結篇1,定義組件實現父子組件傳值

實現 efault () 語法 前端 bsp component 定義 我們

前端時間學習了vue,這幾天開始入手react了。

react項目搭建起來之後,我們一定會定義很多個組件。同樣的也會涉及到父子組件的傳值。今天來整理一下這個知識。

1,定義子組件步驟

  1,引入react。

import React,{Component} from ‘react‘;
import ‘./style.less‘;

  2,寫組件並export default導出去用到了es6中的calss語法,提前說一下,this.props.content用來接收父組件中content的值,this.props.say用以向父組件傳值執行負組件的say方法。

export default
class test extends Component { constructor() { super(); } render() { return ( <div> <h1 onclick={this.props.say}>hello world</h1> <p>{this.props.content}</p> </div> ) }

3.在父組件中引入子組件並且渲染同時傳遞給子組件值,接受子組件傳遞過來的方法。

import React, {Component} from ‘react‘;
import  {render} from ‘react-dom‘;
import test from ‘./components/test/Test‘;
class Main extends Component{
  constructor(){
     super();    
  }  
 move(e){
    console.log(event.target);  
    }
 render(){
   return(
      
<div> <Main move={this.move.bind(this) content=‘666‘}><Main/> </div> ) } } render(<Main/>,document.getElementById(‘#app‘));

react基礎總結篇1,定義組件實現父子組件傳值