How to pass value to props from component and set state

Collin

I'm new in react and try to pass a value from the parent component to the child component to the props and store the value in the state. But it doesn't even call the console.log statements

This is my function to change the string by clicking on the button

let actionToPerform = "";

function changeEdit(){
    if(actionToPerform === 'edit'){
        actionToPerform = 'new'
    }else{
        actionToPerform = 'edit'
    }
}

In the parent component, in render I have this:

<Edit action={actionToPerform}
                    />

Child component

import React from 'react'; import * as styles from './edit.module.css';

export default class Edit extends React.Component {

    constructor(props){
        super(props);
        this.state = {actionToPerform: this.props.actionToPerform}
        console.log("props:" + props)
        console.log("parsed state: " + this.state)
    }

    showContent = ()=>{
        if(this.state.actionToPerform == "edit"){
            return <div>Shoppinliste bearbeiten</div>
        }
    }

   render() {
       return (
          this.showContent
       )
   }
}

my goal is, that based on the state which is change by clicking on the button, to show the div or not.

Tarun Yadav

You are passing action props to the child, but parsing actionToPerform. You need to parse action in the child.

Console log should be outside the constructor.

export default class Edit extends React.Component {

constructor(props){
    super(props);
    this.state = {actionToPerform: this.props.action}
   
}



showContent = ()=>{
    console.log("props:" + props)
    console.log("parsed state: " + this.state)

    if(this.state.actionToPerform == "edit"){
        return <div>Shoppinliste bearbeiten</div>
    }
}

 render() {
   return (
      this.showContent
   )
  }
  }

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 state from props that is passed to the component in react?

pass object from state as props to child component

How to pass async state to child component props?

How to pass value of state variable from one component to a sibling component?

Set State with Props from Parent Component

How to pass props from component to another component

Set state in React component from props and update when props change?

How to pass the props when set ref for a component

How pass state value like a props

How to conditionally pass value in props based on state

How to pass the state value of a component to another component?

How to pass a Component and his value as props

How to pass props without value to component

How can I correctly pass state as props from one component to another?

How can I pass props from a functional parent component that uses the state hook?

How to pass the props to Svelte component from Javascript?

How to pass props from unrelated component

how to pass component props from React to Redux?

How React pass props from api to component?

Unable to pass props from parent to child and save it in state of child component

Set props as state in child component

React: How do I pass props to state component?

How to pass props to a component

How to pass props to component

How can I pass state from one component to set another state?

Set state from props change in react functional component?

How do I pass a value from an array defined in state into a component which beholds a component?

How to pass props from functional component to class component

How to call a react component from another component and pass required props