反応コンポーネントを使用してdivにclassNameを追加しようとしています。これは私が持っているものです。私はかなり新しく反応している、btw。私はthis.state.condition
としてそれを続ければ、その中の三条件を持っているdiv要素についてはreact.jsの状態を変更するためのonMouseEnterを取得できません
class PrimaryNavigation extends Component {
constructor(props) {
super(props);
this.state = {
condition: false
};
}
onMouseEnterHandler() {
this.setState({
condition: !this.state.condition
});
}
clickHandlerFor(component) {
return (e) => {
if (typeof this.props.onClick !== 'function') {
return;
}
e.preventDefault();
var componentData = this.componentDataFor(component);
this.props.onClick(e, componentData);
};
}
componentDataFor(component) {
...
}
render(): ReactElement {
...
return (
<div className='PrimaryNavigation'>
<nav className='PrimaryNavigation-Links'>
<ul>
...
<li className="dropdown" onMouseEnter={this.onMouseEnterHandler}>
<Link to='/account' className='PrimaryNavigation-link dropbtn'>
<span className='PrimaryNavigation-label'><FormattedMessage id='navigation.my_account' /></span>
</Link>
<div id="myDropdown" className={this.state.condition ? "dropdown-content show" : "dropdown-content" }>
<div className="dropdown-box">
...
</div>
</div>
</li>
</ul>
</nav>
</div>
);
}
}
は、"dropdown-content"
のクラス名が表示されます。 !this.state.condition
に変更すると、"dropdown-content show"
のクラス名が表示されます。私が抱えている問題は国家を変えているということです。私のonMouseEnterHandler()
機能では、私は条件が真であるように変更しました。まだ何も。
どうすればよいですか?また、私の質問を誤って記述した場合は、どのような編集提案も高く評価されます。
あなたは 'onMouseEnterHandler'それは今までそれがトリガされていることをログんではconsole.logを置く場合:
は、関連するが、ここで文書を反応させるのか? – finalfreq
'onMouseEnterHandler'は正しいコンテキストにバインドされていますか? –