Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

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 (<div><ChildComponent1 myFunction={this.myFunction}/></div>)
    }
}

class ChildComponent1{
      render(){
  return (<div><ChildComponent2 myFunction={this.props.myFunction}/></div>)
    }
}

class ChildComponent2{
      render(){
  return (<Button onClick={()=>this.props.myFunction(param)}>SomeButton</Button>)
    }
}
Comment

function pass as prop

import React from 'react';

class NewComponent extends React.Component {

    clickMe = () => {
        this.props.clickMe()
    }

    render() {
        return (
            <div>
                <button onClick={this.clickMe}>Click Me!</button>
            </div>
        )
    }
}

export default NewComponent;
Comment

PREVIOUS NEXT
Code Example
Javascript :: open bootstrap modal using vanilla js 
Javascript :: double exclamation mark js 
Javascript :: _.pluck 
Javascript :: react portals 
Javascript :: js add event listener 
Javascript :: js console log function code 
Javascript :: Recursion In DOM 
Javascript :: path object d3.js 
Javascript :: How to add js file to a site through url 
Javascript :: create your own programming language in javascript 
Javascript :: Remove all falsy values from an array 
Javascript :: react js photo gallery 
Javascript :: error: Error: Unable to resolve module `crypto` from `node_modulescrypto-jscore.js`: crypto could not be found within the project. 
Javascript :: take one character in end of string javascript 
Javascript :: For-each over an array in JavaScript 
Javascript :: concatenate arrays javascript 
Javascript :: AJAX - The XMLHttpRequest Object 
Javascript :: ?. js 
Javascript :: queryselector multiple attributes 
Javascript :: conditional style react 
Javascript :: service worker.js 
Javascript :: example custom theme material ui 
Javascript :: javascript unicode 
Javascript :: jq append value to array 
Javascript :: debounce reactjs 
Javascript :: promise in js 
Javascript :: js arrays in arrays 
Javascript :: polymer js tutorial 
Javascript :: factory function in javascript 
Javascript :: javascript loop object key value 
ADD CONTENT
Topic
Content
Source link
Name
1+8 =