Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

react pass props to child

const Parent = props => {
	return <Child {...props} />
}
Comment

react passing all props to child

<Component propOne={propOne} propTwo={propTwo} {...props}>
	{children}
</Component>
Comment

react pass props to children

import React, { Children, isValidElement, cloneElement } from 'react';

const Child = ({ doSomething, value }) => (
  <div onClick={() => doSomething(value)}>Click Me</div>
);

function Parent({ children }) {
  function doSomething(value) {
    console.log('doSomething called by child with value:', value);
  }

  render() {
    const childrenWithProps = Children.map(children, child => {
      // Checking isValidElement is the safe way and avoids a TS error too.
      if (isValidElement(child)) {
        return cloneElement(child, { doSomething })
      }

      return child;
    });

    return <div>{childrenWithProps}</div>
  }
};

ReactDOM.render(
  <Parent>
    <Child value="1" />
    <Child value="2" />
  </Parent>,
  document.getElementById('container')
);
Comment

pass props from child to parent

/* Parent */
	// The Parent creates the function to be used by the Child and
	// whenever the Child calls the function, it is triggered from
	// the Parent.

const Parent = () => {
  const saveProjectFunction = data => {
    // Recieving data(object) from the Child, instead of passing
    // the object to the Child.
    const modData = {
    	id: 1,
      	...data
    }
  	console.log(`From the Parent ${modData}`);
  }
  return(
		<Child onSaveProject={saveProjectFunction}/>
	)
}

// NOTE: YOU CAN'T SKIP INTERMEDIATE COMPONENT
// The way you pass down through each component, is the same way
// You pass up without skipping a component

/* Child */
  // The child basically calls the function from the parent which was
  // pass in as props, but the function lives and is being used in
  // the parent.

const Child = ({ onSaveProject }) => {
  const sendData = () => {
  	const data = {
      	name: "kouqhar",
      	sport: "basketball"
    }
    // Sending the created data(object) to the Parent instead of
    // Recieving data from the Parent.
    onSaveProject(data)
  }
	return(
  		<button onClick={sendData}><button/>
      )
}

// With love @kouqhar
Comment

PREVIOUS NEXT
Code Example
Javascript :: redux extension 
Javascript :: javascript keyup event enter key 
Javascript :: regex expression to match domain name 
Javascript :: multiple case switch javascript 
Javascript :: how to get common elements from two array in javascript using lodash 
Javascript :: js execute string 
Javascript :: js object loop 
Javascript :: js add text after div 
Javascript :: canactivate angular 
Javascript :: regex remove duplicates 
Javascript :: change image src using jquery 
Javascript :: make input not editable for user js 
Javascript :: how to create a folder using fs in node js 
Javascript :: js key value array 
Javascript :: install vue by CDN 
Javascript :: create a json object in javascript 
Javascript :: jquery select first matching element 
Javascript :: create react app with vite 
Javascript :: how to assert element attributes in cypress 
Javascript :: react scroll to bottom 
Javascript :: react dont render until loaded 
Javascript :: socket io emit to socket id 
Javascript :: validationResult is not defined 
Javascript :: javascript loop x times 
Javascript :: use style in react 
Javascript :: gql TypeError: Object(...) is not a function 
Javascript :: how to remove the top border from table react bootstrap 
Javascript :: getDataSnapshotFirebase 
Javascript :: hexstring to rgb array js 
Javascript :: inheritance in javascript 
ADD CONTENT
Topic
Content
Source link
Name
1+2 =