Change parent component props/state from child

AnthonyDa

I'm currently learning REACT to create a web app. In this app, I have a list of selectedCharacters in the parent's state and in each child component I have an input for the player name. I'm struggling to update the player name in the parent's state.

class Game extends React.Component {
  state = {
    selectedCharacters: [{"name":"Loup Garou","imgName":"base_loup.png","uniqueKey":"loup","playerName":""},{"uniqueKey":"voyante","imgName":"base_voyante.png","name":"Voyante","maxInGame":1,"left":1}]
  };
  changePlayerName = (char, newName) => {
    char.playerName = newName;
  };
  render() {
    const { selectedCharacters } = this.state;
    return(<CharactersSelection selectedCharacters={selectedCharacters} />);  
  }
}

const CharactersSelection = props => {
  return (
    <div className="row col-12 char-list">      
      <div className="col-md-9 col-xl-10 char-selected pad-r-10 pad-l-10">
        <div className="row char-selected-content">
          {props.selectedCharacters.map((char, i) => (
            <CharacterCardSelected key={i} imgName={char.imgName} name={char.name} playerName={char.playerName}/>
          ))}
        </div>
      </div>
    </div>
  );
};

const CharacterCardSelected = props => {
  return (
    <div className="d-flex char-card-selected" id={props.id}>
      <img alt={props.imgName} className="char-img-sm" src=require("../../public/images/" + props.imgName)}/>
      <div className="char-card-selected-txt">
        <div>
          <input
            type="text"
            className="form-control player-name"
            placeholder="Nom joueur..."
            value={props.playerName}
            onChange={e => {console.log(e)}}
          />
        </div>
      </div>
    </div>
  );
};

Help will be appreciated.

samee

What you will do is pass the changePlayerName function as a prop to CharactersSelection and from there pass it further down to CharacterCardSelected component. Now onChange of CharacterCardSelected call the method this.props.changePlayerName(name);

class Game extends React.Component {
  state = {
    selectedCharacters: [{"name":"Loup Garou","imgName":"base_loup.png","uniqueKey":"loup","playerName":""},{"uniqueKey":"voyante","imgName":"base_voyante.png","name":"Voyante","maxInGame":1,"left":1}]
  };
  changePlayerName = (char, newName) => {
    char.playerName = newName;
  };
  render() {
    const { selectedCharacters } = this.state;
    return(<CharactersSelection selectedCharacters={selectedCharacters} onChange={this.changePlayerName} />);  
  }
}

const CharactersSelection = props => {
  return (
    <div className="row col-12 char-list">      
      <div className="col-md-9 col-xl-10 char-selected pad-r-10 pad-l-10">
        <div className="row char-selected-content">
          {props.selectedCharacters.map((char, i) => (
            <CharacterCardSelected key={i} imgName={char.imgName} name={char.name} playerName={char.playerName} onChange={(newName) => {props.onChange(char,newName)}}/>
          ))}
        </div>
      </div>
    </div>
  );
};

const CharacterCardSelected = props => {
  return (
    <div className="d-flex char-card-selected" id={props.id}>
      <img alt={props.imgName} className="char-img-sm" src=require("../../public/images/" + props.imgName)}/>
      <div className="char-card-selected-txt">
        <div>
          <input
            type="text"
            className="form-control player-name"
            placeholder="Nom joueur..."
            value={props.playerName}
            onChange={e => {
              console.log(e);
              props.onChange(e.target.value)
            }}
          />
        </div>
      </div>
    </div>
  );
}; 

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Child component is not updating from parent state change

How to change parent data from a child component?

How to change useState in functional component (child) from class component (parent)

How to change a parent component's varibale from a child component

React js change child component's state from parent component

Angular change child component style from parent component but not globally

How can I change the state of a parent component from a child component?

How to change title from child component to parent component without hooks?

How to change image of child component button and navigate from parent component

Parent component doesn't change from child component

How to change the child state component from parent in functional component

Change state in parent component from child, then pass that state as property and change state in child component

How would I change the state in the parent component from the child in React?

Change parent state from child component doesn't work

Prevent child component from rerendering based on change of state in parent

change parent props from child component in vuejs2

Change Parent State from Child Component in React (No Redux)

Trying to change state in Parent from Child component React Hooks

How to change attribute of a child component from parent in React?

Change ref of the parent from child component using Vue 3

How to pass parent value to child on first render and change parent component value from Child?

Change state of parent component in child component

React Native: How to change parent component's state from child component and re-render parent?

react: How to change the state of a child component(function component, not class component) from its parent?

Update child component from parent

Event from parent to child component

Can't change State from child component even after passing a function from parent component

Trigger/update data change from child component to parent component in Quasar/Vue incase of child component has list of items

Update parent component from child component