将 parent 组件从 child 更改为 props/state
Change parent component props/state from child
我目前正在学习 REACT 以创建网络应用程序。
在这个应用程序中,我有一个处于 parent 状态的 selectedCharacters 列表,在每个 child 组件中我都有一个玩家名称输入。
我正在努力更新 parent 状态下的玩家名称。
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>
);
};
我们将不胜感激。
尝试在父组件中创建一个函数,然后将其传递给子组件。将名称分配给父级中的状态变量,并将其传递给子组件。
扩展迈克尔的回答:
class Parent extends React.Component {
state = { someValue: "" }
onChangeValue = (event) => {
this.setState({ someValue: event.target.value })
}
render () {
const { someValue } = this.state
return (
<Child onChangeValue={this.onChangeValue} value{someValue}/>
)
}
}
class Child extends React.Component {
render() {
return (
<input
onChange={this.props.onChangeValue}
value={this.props.value}
/>
)
}
}
这是 React 中的一种常见模式,称为 Lifting State Up
您要做的是将 changePlayerName 函数作为 prop 传递给 CharactersSelection,然后从那里进一步向下传递给 CharacterCardSelected 组件。现在 CharacterCardSelected 的 onChange 调用方法 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>
);
};
将changePlayerName函数传递给最底层child。从那里您需要触发 parent 组件的 setState,以更改玩家的名字。
class Game extends React.Component {
constructor(props) {
super(props);
this.state = {
selectedCharacters: [{
"name": "LoupGarou",
"imgName": "base_loup.png",
"uniqueKey": "loup",
"playerName": ""
}, {
"uniqueKey": "voyante",
"imgName": "base_voyante.png",
"name": "Voyante",
"maxInGame": 1,
"left": 1
}]
}
}
changePlayerName = (index, newName) => {
this.setState({
selectedCharacters[index].playerName: newName
});
};
render() {
const {
selectedCharacters
} = this.state;
return ( < CharactersSelection selectedCharacters = {
selectedCharacters
}
changePlayerName = {
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, index) => ( <
CharacterCardSelected key = {
index
}
imgName = {
char.imgName
}
changePlayerName = {
props.changePlayerName
}
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) => props.changePlayerName(props.key, e.target.value)
}
/> < /div > < /div> < /div > );
};
我目前正在学习 REACT 以创建网络应用程序。 在这个应用程序中,我有一个处于 parent 状态的 selectedCharacters 列表,在每个 child 组件中我都有一个玩家名称输入。 我正在努力更新 parent 状态下的玩家名称。
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>
);
};
我们将不胜感激。
尝试在父组件中创建一个函数,然后将其传递给子组件。将名称分配给父级中的状态变量,并将其传递给子组件。
扩展迈克尔的回答:
class Parent extends React.Component {
state = { someValue: "" }
onChangeValue = (event) => {
this.setState({ someValue: event.target.value })
}
render () {
const { someValue } = this.state
return (
<Child onChangeValue={this.onChangeValue} value{someValue}/>
)
}
}
class Child extends React.Component {
render() {
return (
<input
onChange={this.props.onChangeValue}
value={this.props.value}
/>
)
}
}
这是 React 中的一种常见模式,称为 Lifting State Up
您要做的是将 changePlayerName 函数作为 prop 传递给 CharactersSelection,然后从那里进一步向下传递给 CharacterCardSelected 组件。现在 CharacterCardSelected 的 onChange 调用方法 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>
);
};
将changePlayerName函数传递给最底层child。从那里您需要触发 parent 组件的 setState,以更改玩家的名字。
class Game extends React.Component {
constructor(props) {
super(props);
this.state = {
selectedCharacters: [{
"name": "LoupGarou",
"imgName": "base_loup.png",
"uniqueKey": "loup",
"playerName": ""
}, {
"uniqueKey": "voyante",
"imgName": "base_voyante.png",
"name": "Voyante",
"maxInGame": 1,
"left": 1
}]
}
}
changePlayerName = (index, newName) => {
this.setState({
selectedCharacters[index].playerName: newName
});
};
render() {
const {
selectedCharacters
} = this.state;
return ( < CharactersSelection selectedCharacters = {
selectedCharacters
}
changePlayerName = {
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, index) => ( <
CharacterCardSelected key = {
index
}
imgName = {
char.imgName
}
changePlayerName = {
props.changePlayerName
}
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) => props.changePlayerName(props.key, e.target.value)
}
/> < /div > < /div> < /div > );
};