2016-12-06 3 views
1

反応コンポーネントを使用して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()機能では、私は条件が真であるように変更しました。まだ何も。

どうすればよいですか?また、私の質問を誤って記述した場合は、どのような編集提案も高く評価されます。

+0

あなたは 'onMouseEnterHandler'それは今までそれがトリガされていることをログんではconsole.logを置く場合:

class PrimaryNavigation extends Component { constructor(props) { super(props); this.state = { condition: false }; this.onMouseEnterHandler = this.onMouseEnterHandler.bind(this); } 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> ); } } 

は、関連するが、ここで文書を反応させるのか? – finalfreq

+0

'onMouseEnterHandler'は正しいコンテキストにバインドされていますか? –

答えて

2

コンストラクタ内のコンポーネントインスタンスにonMouseEnterHandler()メソッドをバインドする必要があります。そうでない場合は、thisコンテキストはコンポーネントにならず、は未定義になります。更新されたコードを参照してください:https://facebook.github.io/react/blog/2015/01/27/react-v0.13.0-beta-1.html#autobinding

+0

ダニエル、ありがとう!私は文書を調べ、拘束力をチェックします。 – Robby

+1

これは、イベントコールバック(クリック、マウス入力、ウィンドウサイズ変更、キー押下など)として呼び出すメソッドに適用されることに注意してください。これらのコールバックをインスタンスにバインドする必要があります。多くの場合、開発者は、構築中にバインドされるべき関数名の配列を使用することになるでしょう: '['handleClick'、 'someOtherFunction']。forEach(fn => this [fn] = this [fn] .bind(this)); ' –

+0

ちょっとライアン、ノートのおかげで。私は、イベントコールバックとしてメソッドを呼びたいときに注意します。 – Robby

関連する問題