2017-11-08 6 views
-1

나는 category select 드롭 다운을 가지고 있으며 현재이 배열에서 가상 데이터를 사용하고 있습니다.ReactJS - onChange 이벤트를 사용하여 매핑 할 수 없습니다.

constructor() { 
    super(); 
    this.state = { 
     search: '', 
     categories: '', 
     products: '', 
     categories: ['Technology', 'Sport', 'Fashion', 'Clothes'], 
    } 

그런 다음, 나는 그것을

<div className="col-lg-4"> 
    <select id="categories" className="form-control"> 
     { 
     this.state.categories.map((i, index) => <option key={index} value={i} >{i}</option>) 
     } 
    </select> 
</div> 

을 통해 작동이 방법을 반복 매핑을 사용합니다. 그러나, 나는 선택한 카테고리에 따라 상태를 변경하려면,하지만 난 selectonChange={handleCategories}를 추가 할 때 오류 얻을 : (I 객체로 매핑을 사용하는 동안 사람들이 일반적으로 문제가 눈치 만의 작품을 인터넷 검색을하여

Uncaught TypeError: this.state.categories.map is not a function 

을 배열에 대한), 그러나 내 문제에 대한 링크를 찾을 수 없습니다.

작동하지 :

<div className="col-lg-4"> 
    <select id="categories" onChange={handleCategories} className="form-control"> 
     { 
     this.state.categories.map((i, index) => <option key={index} >{i}</option>) 
     } 
    </select> 
</div> 
+0

업데이트 코드로 질문을 알 수 없습니다 일. 'onChange' 함수는 어떻게 전달합니까? –

+0

@Prakash 업데이트 – Seinfeld

+1

옵션을 변경하면 오류가 발생합니까? 그렇다면'handleCategories' 함수를 보여줍니다. –

답변

-1

당신은 this.handleCategories.bind(this)를 사용하여 인스턴스에 기능을 결합 할 필요가, 그렇지 않으면 함수가되지 않습니다 state.categories

+0

나는 this.handleCategories = this.handleCategories.bind (이)를 이처럼 묶었지만, 어쨌든 나는 오류가 발생한 정확한 위치를 메쏘드에 넣었다. – Seinfeld

+0

@ Seinfeld 당신이 문제를 해결했다고 말하고 있습니까? – brandNew

+0

예,'target.value'를 사용하는 것을 잊었습니다. – Seinfeld