react pass function as prop code example

Example 1: defining functions in react

export default class Archive extends React.Component { 

    saySomething(something) {
        console.log(something);
    }

    handleClick(e) {
        this.saySomething("element clicked");
    }

    componentDidMount() {
        this.saySomething("component did mount");
    }

    render() {
        return 

Example 2: pass props in react

/* PASSING THE PROPS to the 'Greeting' component */
const expression = 'Happy';
 // statement and expression are the props (ie. arguments) we are passing to Greeting component

/* USING THE PROPS in the child component */
class Greeting extends Component {
  render() {
    return 

{this.props.statement} I am feeling {this.props.expression} today!

; } }

Example 3: pass function with parameter as prop

class SomeComponent extends Component{

    constructor(props){
        super(props);
        //does whatever stuff        
        this.myFunction = this.myFunction.bind(this);

    }

    //(only applicable to raw and normal forms)
    myFunction(param){
        console.log('do something: ', param);
    }

    render(){
     return (
) } } class ChildComponent1{ render(){ return (
) } } class ChildComponent2{ render(){ return () } }

Example 4: react native function

class Foo extends Component {
  handleClick() {
    console.log('Click happened');
  }
  render() {
    return ;
  }
}

Example 5: pass params react js

//pass color from parent -> child

App.js -> ColorPicker (Components/ColorPicker.js)

/// parent App.js
import ColorPicker from './Component/ColorPicker';

class App extends Component {
  
   constructor(props) {
        super(props);
        this.state = {
            color:  'red',
        };
    }
  
   render() {
        return (
			
) } }export default App; /// child ColorPicker import React, { PureComponent } from 'react'; class ColorPicker extends PureComponent { constructor(props) { super(props); this.state = { colors: ['red', 'green', 'blue', 'yellow', 'purple'], }; } render() { var elementColors = this.state.colors.map((c, index) => { return }); } export default ColorPicker; // --------------------------------------- //pass params from child -> parent // onClick => setActiveColor -> this.props.onReceiverColor(c); ColorPicker (Components/ColorPicker.js) -> App.js /// child ColorPicker import React, { PureComponent } from 'react'; class ColorPicker extends PureComponent { constructor(props) { super(props); this.state = { colors: ['red', 'green', 'blue', 'yellow', 'purple'], }; } setActiveColor = (c) => { this.props.onReceiverColor(c); } render() { var elementColors = this.state.colors.map((c, index) => { return this.setActiveColor(c) } // click into color c will pass params c to Bai11Practice.js > }); } export default ColorPicker; // Parent import React, { Component } from 'react'; import ColorPicker from './Component/ColorPicker'; class App extends Component { onSetColor(c) { alert(c); } render() { return ( ); } }export default App;

Tags:

Misc Example