Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change props to state in React Hooks?

In Simple react class component we used to change the props to state this way:

constructor(props) {     super(props)      this.state = {       pitch: props.booking.pitch,       email: props.booking.email,       firstName: props.booking.firstName,       arrivalDate: props.booking.arrivalDate     } }  

But I am not aware of how to do it in new feature as Hooks, but I am trying to do it this way.

const GenerateDescHook = ({ description: initialDesc }) => {   const [description, setDescription] = useState(null)  useEffect(() => {     setDescription(initialDesc)   }, {})   function handleChangeVariance(newVariance) {     setDescription({       ...description,       template: {         ...description.template,         variance_name: newVariance,       },     })   }  } 

Basically, I just need to change description props, which coming from another parent component to turn to state. Pls, could you show me how to do it in new way as Hooks way?

like image 568
Feruza Avatar asked Jan 10 '19 14:01

Feruza


People also ask

How do you pass props to state in React hooks?

Passing props to state using useState Hooks import React, { useState } from 'react'; const Profile = props => { const [profileState, setProfileState] = useState(props); return ( <div> <p> <strong>Name:</strong> {profileState.name} </p> <p> <strong>Email:</strong> {profileState.

How do I change props to state in React?

Component { state = { description: '' } constructor (props) => { const { description } = props; this. state = {description}; } render () { const {state: { description }} = this; return ( <input type="text" value={description} /> ); } } export default SecondComponent; Update: I changed setState() to this.

How do you change state when props change?

To update state when props change in React:Pass the props as dependencies to the useEffect hook. Every time the props change, the logic in useEffect is reran.

Can React hooks have state?

Hooks are a new addition in React 16.8. They let you use state and other React features without writing a class. Read the Motivation to learn why we're introducing Hooks to React.


2 Answers

The problem in your code is with {}. UseEffect hook expects and array, not an object. Use [initialDesc] instead.

This is how you reset component state when props change

const GenerateDescHook = ({ description: initialDesc }) => {   const [description, setDescription] = useState(null)    useEffect(() => {     setDescription(initialDesc)   }, [initialDesc]); } 

This is how you initialize component state to prop value on first render only

const GenerateDescHook = ({ description: initialDesc }) => {   const [description, setDescription] = useState(initialDesc); } 
like image 143
Jkarttunen Avatar answered Sep 22 '22 21:09

Jkarttunen


You can pass your initial state as first argument to useState like this:

const GenerateDescHook = ({ description: initialDesc }) => {   const [description, setDescription] = useState(initialDesc)    ... 
like image 26
Michiel Dral Avatar answered Sep 21 '22 21:09

Michiel Dral