2017-05-03 14 views
0

私の次のスニペットを考えてみましょう。今、ボタンの上にそれをクリックしDIV-3 AnotherComponent.TheのURLをロードするだけで開き、「http://localhost:3000/de」すなわちIndexroot共有可能なパネルのURLに反応する

私は何を達成したいことはある:私は「http://localhost:3000/de/?open」をヒットした場合、私はパネルはすなわちDIV-3たいすでに開いています。

class App extends React.Component { 
 
    constructor(props) { 
 
     super(props); 
 

 
     this.state = { 
 
      
 
      showThird: false 
 
     } 
 

 
     this.showDivThree = this.showDivThree.bind(this) 
 
     /*if(props.location.search=="?open"){ 
 
      this.showDivThree() 
 
     }*/ 
 
    } 
 

 
    showDivThree() { 
 

 
     this.setState(prevState => ({ showSecond: false, showThird: !prevState.showThird})) 
 
     console.log(this.state) 
 
    } 
 
    
 
    render() { 
 
    return (
 
     <div className={'wrapper' + (this.state.showThird ? ' show' : '')}> 
 

 
       <div className="one">one 
 
        
 

 
        {/* Show third */} 
 
        <div> 
 
         <button onClick={this.showDivThree}>{this.state.showThird ? 'hideThird' : 'showThird'}</button> 
 
        </div> 
 
       </div> 
 

 
       <div className="three">three 
 
        <div> 
 
         <button onClick={this.showDivThree}>{this.state.showThird ? 'hideThird' : 'showThird'}</button> 
 
         
 
         <AnotherComponent /> 
 
        </div> 
 
       </div> 
 

 
      </div> 
 
    ) 
 
    } 
 
} 
 

 
class AnotherComponent extends React.Component { 
 
    constructor(props) { 
 
     super(props); 
 

 
     this.state = { 
 
      
 
     } 
 
     
 
    } 
 

 
    
 
    render() { 
 
    return (
 
     <div> 
 
     <h4>Another component</h4> 
 
     </div> 
 
    ) 
 
    } 
 
} 
 

 
ReactDOM.render(
 
    <App />, 
 
    document.getElementById('root') 
 
);
.wrapper { 
 
    overflow: hidden; 
 
    white-space: nowrap; 
 
} 
 

 
.one, .two, .three { 
 
    background: #333; 
 
    border: 2px solid #787567; 
 
    box-sizing: border-box; 
 
    color: #fff; 
 
    display: inline-block; 
 
    font-family: arial; 
 
    overflow: hidden; 
 
    padding: 20px; 
 
    text-align: center; 
 
    transition: border 0.2s, padding 0.2s, width 0.2s; 
 
    min-height: 50vh; 
 

 
} 
 

 
.one { 
 
    width: 100%; 
 
} 
 
.two { 
 
    border-width: 2px 0; 
 
    padding: 20px 0; 
 
    width: 0; 
 
} 
 

 
.three { 
 
    border-width: 2px 0; 
 
    padding: 20px 0; 
 
    width: 0; 
 
} 
 

 
.show .one, .show .two, .show .three { 
 
    border-width: 2px; 
 
    padding: 20px; 
 
    width: 50%; 
 
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> 
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script> 
 
<script src="https://unpkg.com/react-router/umd/react-router.min.js"></script> 
 

 
<div id="root"></div>

私はそれが存在するならば、私は単にDIV-3を開く関数を呼び出す、私はprops.locationから文字列を検索読み取ったコードをコメントしています。しかし、私はdivを開くために条件が混在しているので、何とか動作していません。

どうすればこの問題を解決できますか?

答えて

0

contructorsetStateshowDivThreeメソッド呼び出しはsetState)を呼び出すことはできません。コンポーネントがまだ呼び出されていないためです。詳細は SO answerをご確認ください。
あなたがconstructorからsetStateを部品が実装された直後に呼び出され、その中にあなたが安全に使用することができますcomponentDidMount方法にURL検索文字列をチェックif文を移動する必要があります。他に

class App extends React.Component { 
    constructor(props) { 
     super(props); 

     this.state = { 
      showThird: false 
     }; 

     this.showDivThree = this.showDivThree.bind(this) 
    } 
    componentDidMount() { 
     if (props.location.search == "?open") { 
      this.showDivThree(); 
     } 
    } 
    ... 
} 

、私はあなたのURLがあるべきだと思います検索クエリの前にスラッシュを入れないでください。したがって、http://localhost:3000/de/?openの代わりにhttp://localhost:3000/de?openにする必要があります。

関連する問題