handleInputChange = (e) => { const name = e.target.value; this.setState({ childName: name }) }
handleParentValueChange = (value) => { this.setState({ name: value }) }
render() { return ( <div> <h2>I'm the parent page</h2> <p>Receive from child: {this.state.name}</p> Enter data to pass to child: <Input onChange={this.handleInputChange}></Input> <Child name={this.state.childName} onParentNameChange={this.handleParentValueChange}></Child> </div> ) } }
export default Parent;
Child.js
import { PureComponent } from 'react'; import { Input } from 'antd';
render() { return ( <div style={{backgroundColor: "lightgray", padding: "10px 10px 10px 10px"}}> <h2>I'm the child page</h2> <p>Receive from parent by props: {this.props.name}</p> Enter data to pass to parent: <Input onChange={this.handleInputChange}></Input> </div> ) } }
export default Child;
Pass functions between a parent and a child component
Pass functions to child components
If you need to have access to the parent component in the handler, you also need to bind the function to the component instance (see below).
There are several ways to make sure functions have access to component attributes like this.props and this.state.
Note: Make sure you aren’t calling the function when you pass it to the component
render() { // Wrong: handleClick is called instead of passed as a reference! // The function being called every time the component renders. return <button onClick={this.handleClick()}>Click Me</button> }
Note: Make sure you aren’t calling the function when you pass it to the component
render() { // Wrong: handleClick is called instead of passed as a reference! // The function being called every time the component renders. return <button onClick={this.handleClick()}>Click Me</button> }
Note: Using an arrow function in render creates a new function each time the component renders, which may break optimizations based on strict identity comparison.
Call child functions in a parent component
refs
Previously, refs were only supported for Class-based components. With the advent of React Hooks, that’s no longer the case.
Modern React with Hooks (v16.8+)
Hook parent and hook child (Functional Component Solution)
const Parent = () => { // In order to gain access to the child component instance, // you need to assign it to a `ref`, so we call `useRef()` to get one const childRef = useRef();
// We need to wrap component in `forwardRef` in order to gain // access to the ref object that is assigned using the `ref` prop. // This ref is passed as the second parameter to the function component. const Child = forwardRef((props, ref) => {
// The component instance will be extended // with whatever you return from the callback passed // as the second argument useImperativeHandle(ref, () => ({ getAlert() { alert("getAlert from Child"); } })); return <h1>Hi</h1>; });
Legacy API using Class Components (>= react@16.4)
Class parent and class child (Class Component Solution)
/** * This is a reducer - a function that takes a current state value and an * action object describing "what happened", and returns a new state value. * A reducer's function signature is: (state, action) => newState * * The Redux state should contain only plain JS objects, arrays, and primitives. * The root state value is usually an object. It's important that you should * not mutate the state object, but return a new object if the state changes. * * You can use any conditional logic you want in a reducer. In this example, * we use a switch statement, but it's not required. */ function counterReducer(state = { value: 0 }, action) { switch (action.type) { case 'counter/incremented': return { value: state.value + 1 } case 'counter/decremented': return { value: state.value - 1 } default: return state } }
// Create a Redux store holding the state of your app. // Its API is { subscribe, dispatch, getState }. let store = createStore(counterReducer)
// You can use subscribe() to update the UI in response to state changes. // Normally you'd use a view binding library (e.g. React Redux) rather than subscribe() directly. // There may be additional use cases where it's helpful to subscribe as well.
// The only way to mutate the internal state is to dispatch an action. // The actions can be serialized, logged or stored and later replayed. store.dispatch({ type: 'counter/incremented' }) // {value: 1} store.dispatch({ type: 'counter/incremented' }) // {value: 2} store.dispatch({ type: 'counter/decremented' }) // {value: 1}
Pass data to components use URL query string: url?field=value&field2=value2
Get query string parameters
const params = newProxy(newURLSearchParams(window.location.search), { get: (searchParams, prop) => searchParams.get(prop), }); // Get the value of "some_key" in eg "https://example.com/?some_key=some_value" let value = params.some_key; // "some_value"