gpt4 book ai didi

javascript - react 。在 onClick 中调用多个事件处理程序会导致 "Null"或 "undefined"

转载 作者:行者123 更新时间:2023-12-03 03:57:52 24 4
gpt4 key购买 nike

我有两个事件处理程序,一个位于组件内(子组件名为“counter.js”,另一个由 props 传递,即父组件“container.js”)

我想传递给 onClick 事件,但是我一次只能让一个事件工作,而不能同时工作。我尝试编写一个函数来保存这两个事件并使用 onClick,但随后出现错误。

如果我只使用其中一个事件,这两个事件都会起作用,但同时使用这两个事件往往会中断。我可以重新审视我做错的事情吗?

在我的父组件(container.js)中,我将其作为 Prop 传递:

    class Container extends Component {
constructor(props) {
super(props);

this.moveCard = this.moveCard.bind(this);
this.state = {
id: players.id,
totalScore: 0,
countInfo: [],
evilName: '',
color: '#6E68C5',
scoreColor: '#74D8FF',
fontAwe: 'score-icon',
incrementcolor: '',
scoreNameColor: 'white',
glow: '',
buttonStyle: 'count-button-start'
};

this.incrementCountTotal = this.incrementCountTotal.bind(this);
this.decrementCountTotal = this.decrementCountTotal.bind(this);
}

incrementCountTotal() {
this.setState({
totalScore: this.state.totalScore + 1
});
}

decrementCountTotal() {
this.setState({
totalScore: this.state.totalScore - 1
});
}

moveCard(dragIndex, hoverIndex) {
const { cards } = this.state;
const dragCard = cards[dragIndex];

this.setState(
update(this.state, {
cards: {
$splice: [[dragIndex, 1], [hoverIndex, 0, dragCard]]
}
})
);
}

sortableGroupDecorator = componentBackingInstance => {
if (componentBackingInstance) {
let options = {
group: 'shared'
};
Sortable.create(componentBackingInstance, options);
}
};

render() {
const listPlayers = players.map(player => (
<Counter
key={player.id}
player={player}
name={player.name}
sortableGroupDecorator={this.sortableGroupDecorator}
decrementCountTotal={this.decrementCountTotal}
incrementCountTotal={this.incrementCountTotal}
/>
));
return (
<ContainLeft style={{ alignItems: 'center' }}>
<ProjectTitle>Score Keeper</ProjectTitle>
<Copy>
A sortable list of players that with adjustable scores. Warning, don't go negative!
</Copy>
<div>
<Stats totalScore={this.state.totalScore} players={players} />
{listPlayers}
</div>
</ContainLeft>
);
}
}

export default Container;

然后在我的子组件中,我想在 onClick 区域放置两个事件,但它们中断了。 (目前我在 onClick 中只有一个事件,但我希望 this.incrementCountincrementCountTotal 都能在 onClick 上工作(incrementCountTotal 来自父组件,this.incrementCount在子组件中

class Counter extends Component {
constructor(props) {
super(props);

this.state = {
score: 0,
countInfo: [],
evilName: '',
color: '#6E68C5',
scoreColor: '#74D8FF',
fontAwe: 'score-icon',
incrementcolor: '',
scoreNameColor: 'white',
glow: '',
buttonStyle: 'count-button-start'
};

this.incrementCount = this.incrementCount.bind(this);
this.decrementCount = this.decrementCount.bind(this);
}
incrementCount() {
console.log('pressme');
this.incrementCountTotal;
this.setState({
score: this.state.score + 1
});
if (this.state.score < 0) {
this.setState({ color: '#6E68C5' });
this.setState({ scoreNameColor: 'white' });
this.setState({ buttonStyle: 'count-button-start' });
this.setState({ fontAwe: 'fontAwe: score-icon' });
this.setState({ scoreColor: '#74D8FF' });
this.setState({ evilName: '' });
} else if (this.state.score === 9) {
this.setState({ scoreColor: 'white' });
} else if (this.state.score === 19) {
this.setState({ glow: '1px 2px 5px #74D8FF' });
}
}

decrementCount() {
this.setState({
score: this.state.score - 1
});
if (this.state.score < 0) {
this.setState({ color: '#381919' });
this.setState({ scoreNameColor: '#F79D05' });
this.setState({ buttonStyle: 'count-button-hovering' });
this.setState({ fontAwe: 'fontAwe: score-icon-switch' });
this.setState({ scoreColor: '#F79D05' });
this.setState({ evilName: 'Darth ' });
} else if (this.state.score === -1) {
this.setState({ glow: '1px 2px 5px red' });
} else if (this.state.score === 11) {
this.setState({ scoreColor: '#74D8FF' });
} else if (this.state.score === 21) {
this.setState({ glow: '' });
}
}

render() {
const {
name,
sortableGroupDecorator,
decrementCountTotal,
incrementCountTotal
} = this.props;
return (
<div className="group-list" ref={sortableGroupDecorator}>
<CountCell style={{ background: this.state.color }}>
<Row style={{ alignItems: 'center' }}>
<Col>
<CountButton
className={this.state.buttonStyle}
onClick={this.incrementCount}
>
<Icon
name="icon"
className={'fa fa-plus ' + this.state.fontAwe}
/>
</CountButton>
</Col>
<Col>
<ScoreName style={{ color: this.state.scoreNameColor }}>
{this.state.evilName + name}
</ScoreName>
</Col>
<Col>
<Score
style={{
color: this.state.scoreColor,
textShadow: this.state.glow
}}
>
{this.state.score}
</Score>
</Col>
<Col>
<CountButton
className={this.state.buttonStyle}
onClick={this.decrementCount}
>
<Icon
name="icon"
className={'fa fa-minus ' + this.state.fontAwe}
/>
</CountButton>
</Col>
</Row>
</CountCell>
</div>
);
}
}

Counter.propTypes = {
player: PropTypes.object.isRequired
};

export default Counter;

所以当我添加:

onClick={(this.incrementCount,incrementCountTotal)}incrementCountTotal 有效(第二个参数)

如果我将它与这样的函数内联编写

onClick={event => {
this.incrementCount;
incrementCountTotal;
}}

没有任何效果。

如果我编写一个函数来处理这两者,它会给我未定义

  onClick(event) {
incrementCountTotal, incrementCount;
}

并输入onClick={this.onClick} 我得到:

./src/components/pages/projectpages/dnd2/Counter.js
Line 97: 'incrementCountTotal' is not defined no-undef
Line 97: 'incrementCount' is not defined no-undef

最佳答案

在子组件中,似乎您只需要调用函数(添加括号)。另外,incrementCountTotal 是一个 prop,所以这样调用它:

  onClick={event => {
this.incrementCount();
this.props.incrementCountTotal();
}
}

关于javascript - react 。在 onClick 中调用多个事件处理程序会导致 "Null"或 "undefined",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44856519/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com