Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

react setstate in another component

// myContainer.js
import React, { useState } from 'react'
import MyChild from 'some/path/myChild'

function MyContainer() {
  const [name, setName] = useState('foo')

  return (
    <MyChild name={name} onNameChange={setName} />
  )
}

export default MyContainer

// myChild.js
import React, { useCallback } from 'react'

function MyChild({ name, onNameChange }) {

  const handleInputChange = useCallback(event => {
    onNameChange(event.target.value)
  }, [onNameChange])

  return (
    <div>
      <input type="text" onChange={handleInputChange} value={name} />
      <div>The name is: {name}</div>
    </div>
  )
}

export default MyChild
Comment

PREVIOUS NEXT
Code Example
Javascript :: javascript trigger keypress 
Javascript :: AngularJS how to use btn-group or radio group in list 
Javascript :: making axios call with headers 
Javascript :: jquery event methods 
Javascript :: new Map() collection in react state 
Javascript :: how to append object in array javascript 
Javascript :: angular subscribe on value change 
Javascript :: async map js 
Javascript :: array destructuring js 
Javascript :: react big calendar messages 
Javascript :: javascript console 
Javascript :: how to get csrf token in javascript 
Javascript :: carbon to moment js conversion 
Javascript :: js how to see console day tomorrow 
Javascript :: module.exports multiple functions 
Javascript :: javasciprt set cookie 
Javascript :: jquery vertical scroll 
Javascript :: js overflowy 
Javascript :: js object from array of keys 
Javascript :: react recoil 
Javascript :: javascript regex One or more occurrences of the pattern 
Javascript :: WebPack basic Configuration 
Javascript :: remove duplicate values from array 
Javascript :: deleteicon material ui improt 
Javascript :: javascript apply 
Javascript :: mongoose use unified topology 
Javascript :: capitalize first letter in array of strings javascript 
Javascript :: monaco editor events 
Javascript :: node js clear cache 
Javascript :: clearinterval javascript 
ADD CONTENT
Topic
Content
Source link
Name
4+7 =