FrontEnd/React

[React] map 형식 안에서 if문 사용하기

모야이거였어 2022. 1. 7. 17:56

3항 연산자, if문 두가지 방법이 있음

3항 연산자 사용법

.map(id => {
    return this.props.schema.collectionName.length < 0 ?
        <Expandable>
            <ObjectDisplay
                key={id}
                parentDocumentId={id}
                schema={schema[this.props.schema.collectionName]}
                value={this.props.collection.documents[id]}
            />
        </Expandable>
    :
        <h1>hejsan</h1>
}

 

 

 

if 문 사용법

.map(id => {
    if(this.props.schema.collectionName.length < 0)
        return <Expandable>
                  <ObjectDisplay
                      key={id}
                      parentDocumentId={id}
                      schema={schema[this.props.schema.collectionName]}
                      value={this.props.collection.documents[id]}
                  />
              </Expandable>
    return <h1>hejsan</h1>
}

 

 


 

출처 : https://stackoverflow.com/questions/44969877/if-condition-inside-of-map-react

 

 

If condition inside of map() React

I have a map()function that needs to display views based on a condition. I've looked at the React documentation on how to write conditions and this is how you can write a condition: {if (loggedIn)...

stackoverflow.com