2017-12-27 15 views
0

JSONから項目を読み込み、クリック時に説明付きでドロップダウンリストを切り替えようとしています。私は静的divに順番に要素を表示することができます(例えば:loc1 & desc1, loc2 & desc2)。第2部分(desc)が隠れているときに正しくレンダリングする方法を見つけるのが難しく、loc divをクリックしたときのみ表示されます。ReactJS:JSON要素を順番にマッピングし、クリック時に非表示のdivを表示する方法

loc1 & loc2, desc1 & desc2ではなく、loc1 & desc1, loc2 & desc2と表示されるように、結果をマップするにはどうすればよいですか?

コード:

var places = { 
    library: { 
    location: [ 
     { 
     loc_name: "library1", 
     "desc": "desc1 : Modern and spacious building" 
     }, 
     { 
     loc_name: "library2", 
     "desc": "desc2 : A cosy small building" 
     } 
    ] 
    } 
}; 



function contentClass(isShow) { 
    if (isShow) { 
    return "content"; 
    } 
    return "content invisible"; 
} 

class Toggle extends React.Component { 
    constructor(props) { 
    super(props); 
    this.state = { isShow: false }; 
    this.handleClick = this.handleClick.bind(this); 
    } 

    handleClick() { 
    this.setState(function (prevState) { 
     return { isShow: !prevState.isShow }; 
    }); 
    } 

    render() { 

    const libraries_desc = places.library.location.map((libr) => 
     <div> 
     <p>{libr.desc}</p> 
     </div> 
    ); 
    const lib_names = places.library.location.map((libr) => 
     <div> 
     <p>{libr.loc_name}</p> 
     </div> 
    ); 

    return (
     <div> 
     <div className='control' onClick={this.handleClick}> 
      <h4>{lib_names}</h4> 
      <div className={contentClass(this.state.isShow)}>{libraries_desc}</div> 
     </div> 
     </div> 
    ); 
    } 
} 



render((
    <Toggle /> 
), document.getElementById('root')); 

現在の結果:

library1 
library2 
desc1 : Modern and spacious building 
desc 2 : A cosy small building 

望ましい結果:あなたのToggleコンポーネントをbすべき

library1 
desc1 : Modern and spacious building (hidden but shown when clicked) 

library2 
desc 2 : A cosy small building (hidden but shown when clicked) 

Codesandbox

答えて

1

場所を別のコンポーネントに抽出する可能性があります。それを抽出することによって、各位置はその状態を知る責任がある。あなたの場合、その可視性を意味します(this.state.isShowによって制御されます)。ここで

は、あなたがそれを行うことができ方法は次のとおりです。

import React from 'react'; 
import { render } from 'react-dom'; 

var places = { 
    library: { 
    location: [ 
     { 
     loc_name: "library1", 
     "desc": "Modern and spacious building" 
     }, 
     { 
     loc_name: "library2", 
     "desc": "A cosy small building" 
     } 
    ] 
    } 
}; 

class Location extends React.Component { 
    constructor(props) { 
    super(props); 
    this.state = { isShow: false }; 
    this.handleClick = this.handleClick.bind(this); 
    } 

    handleClick() { 
    this.setState(function (prevState) { 
     return { isShow: !prevState.isShow }; 
    }); 
    } 

    contentClass(isShow) { 
    if (isShow) { 
     return "content"; 
    } 
    return "content invisible"; 
    } 

    render() { 
    return (
     <div className='control' onClick={this.handleClick}> 
     <h4>{this.props.desc}</h4> 
     <div className={this.contentClass(this.state.isShow)}>{this.props.loc_name}</div> 
     </div> 
    ) 
    } 
} 

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

    render() { 
    const locations = places.library.location.map(location => { 
     return <Location {...location} /> 
    }) 

    return (
     <div> 
     {locations} 
     </div> 
    ); 
    } 
} 



render((
    <Toggle /> 
), document.getElementById('root')); 
1

このように。

class Toggle extends React.Component { 
    constructor(props) { 
    super(props); 
    this.state = { 
     isShow: false, 
     id: -1, // initial value 
    }; 
    } 

    handleClick = (id) => { 
    this.setState({ 
     isShow: !this.state.isShow, 
     id: id 
    }); 
    } 

    render() { 
    const { location } = places.library; 
    const { isShow, id } = this.state; 
    return (
     <div className="control"> 
      {location.map((libr, index) => (
      <div key={index} onClick={() => { this.handleClick(index) }}> 
       <p>{libr.loc_name}</p> 
       {(isShow && (id === index)) && <p>{libr.desc}</p>} 
      </div> 
      ))} 
     </div> 
    ); 
    } 
} 

したがって、div要素をクリックしたとき。 clickイベントが​​と呼ばれ、indexをパラメータとして関数に渡します。表示する現在の要素と一緒にisShowをfalseまたはtrueに設定し、逆にtrueに設定すると、this.state.idによって選択されます。したがって、毎回isShowが真であり、this.state.idが配列の要素indexと一致するたびに。あなたの説明は、あなたが望むように隠されるそうでなければ表示されます。

あなたの望む結果は次のようなものになります。

library1 
desc1 : Modern and spacious building (hidden but shown when clicked) 

library2 
desc 2 : A cosy small building (hidden but shown when clicked) 
+0

は、それが適切に閉じていてもかかわらず、なぜそれはそうだろう、location.map((LiBrを、インデックス)のタグを閉じるには、構文エラーを見せて – Amma

+0

それを? map()の最後に '(')がありませんでした。レンダリングメシードのコードをもう一度見てください。 –

+1

それはそれでした。 – Amma

関連する問題