How to set and get TextField value using as props of functional component in react js?

Liam neesan

I am totally new to react, I have sign up form which have 5 five input fields.

I created one functional component for TextField

MyTextField.js

function MyTextField(props) {
    return (
        <TextField 
            label={props.label} 
            name={props.name}
            placeholder={props.placeholder} 
            type={props.type}
            onChange={props.handleOnChange}
            value={props.value}
        ></TextField>
    )
}
export default MyTextField

Signup.js

function SignUp() {

    const [value, setValue] = useState('')

    function FunConfirmPassword(event) {
        setValue = event.target.value //this function is calling, if I comment this line
        console.log('FunConfirmPassword called - password value = '+ value)
    }
    return (
        <MyTextField type="text"  label="Firstname" name="firstName" placeholder="Enter Firstname"></MyTextField>
        <MyTextField type="text"  label="Lastname" name="Lastname" placeholder="Enter Lastname"></MyTextField>
        <MyTextField type="text"  label="emailID" name="emailID" placeholder="Enter emailID"></MyTextField>
        <MyTextField type="password"  label="password" name="password" placeholder="Enter password"></MyTextField>

        <MyTextField handleOnChange={FunConfirmPassword}  type="password"  label="confirmpassword" name="confirmpassword" placeholder="Enter confirmpassword"></MyTextField>

    )
}

I am trying to learn how to set the value of TextField and get the value of same textfield.

At the same time, it must be re-usable function

dns_nx

I've prepared a codepen example for you, where you can see how this works.

https://codepen.io/dns_nx/pen/BaxjdKd

Basically, you set a property to the child component, in which you define the function which is being executed, when called by the child (see property onChange). To set the value in the MyTextField component, you just have to pass the current value of the variable in SignUp to your MyTextField component (see property value):

function SignUp(props) {
   const [firstName, setFirstName] = useState('');      

   ...
   <MyTextField 
       type="text"  
       label="Firstname" 
       name="firstName" 
       placeholder="Enter Firstname"
       value={firstName}
       onChange={(event) => setFirstName(event.target.value}
   />
   ...
}

Then you need to call the function where you need it in your child component (i.e. onChange event) to pass the new value to the parent component (SignUp). The value is set by the property value:

function MyTextField(props) {
   ...
    <TextField 
        ...
        value={props.value}
        onChange={(event) => props.onChange(event)}
        ...
    ></TextField>
}

This is the way to pass values from child components to parent components.

But as larger your application gets, it makes it hard to handle. Therefore I would recommend you to use a state management library like Redux (react-redux).

https://www.npmjs.com/package/react-redux

and use this with the toolkit package

https://www.npmjs.com/package/@reduxjs/toolkit

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to set default value of props if not passed to functional component in NextJS using Typescript

How to pass the props to react functional component using react table and typescript?

How to access props in react functional component using react and typescript?

How to pass an object as props to a child Functional Component in React.js?

How to declare default props in react functional component

How to use generics in props in React in a functional component?

How to Access Props in React Functional Component Using Props.Match.Params

How to correctly set state in a loop in react functional component using useState

How to force React (functional component using hooks) to create a new component from scratch on props change?

How to Get value of select onChange in Functional Component in React

Set state from props change in react functional component?

How does this functional component using react hooks get its parameters?

React: How can I acces to props of a functional component in a class component

React js set state in functional component

How to create a Functional React Component using Cytoscape.js?

How to set data in child component dynamically by using props in React?

How to set component default props on React component

how to get new value of state in props of React component?

How can we get the updated props in the same function, in a react functional component?

Passing Props in React Functional Component

Inherit props from parent functional component in React.js

In a Functional Component, how do you access props from the redux store using react-redux connect?

How can we pass state as props in functional component using navigationOptions in React Native?

React: Changing State in functional component also Change functional component's props value, and its Parent Class State

how to set displayName in a functional component [React]

How to get access to the chart from HighchartsReact component to ResponsiveGridLayout component when I'm using functional react component

Get props value suggestion in custom react component

react hooks component not get new props value

How to use props and navigation together in one functional component in react native