2016-08-29 6 views
3

verifiedという小道具を各マップ関数で渡したいと思っています。Javascript/Reactのmap()への引数として関数と変数を渡す

UPDATE

がrenderContinentsにverifiedを渡すには動作しますが、ときに、このようrenderCountryするパラメータを追加します。

{continent.countries.map(renderCountry(null, verified))} 

私の出力が空白です。しかし、これは動作しませんか?

更新コード:

const renderCities = cities => { 
    return (
     <div> 
     <label> 
      <input 
       onChange={onChange} 
       type="checkbox"/> 
     {cities.name} 
     </label> 
     </div> 
    ); 
    }; 

    const renderCountries = ({country, verified}) => { 
    console.log("came to country"); 
    return (
     <div className="city-location"> 
     <label> 
      <input 
       onChange={onChange} 
       type="checkbox"/> 
      {country.name} 
     </label> 
     {country.cities.map(renderCities)} 
     </div> 
    ); 
    }; 



function onChange(e) { 
    console.log('checkbox verified:', (e.target.verified)); 
} 


class AreasComponent extends Component { 
     constructor(props) { 
     super(props); 
     this.state = { 
     }; 
     this.renderContinents = this.renderContinents.bind(this); 
    } 

    componentWillMount() { 
    this.props.fetchAllAreas(); 
    } 

    renderContinents(verified, continent) { 
    console.log("came to continent"); 
     return(
     <div className="continent-location"> 
      <label> 
      <input 
       onChange={onChange} 
       type="checkbox"/> 
      {continent.name} 
      </label> 

      {continent.countries.map(renderCountries(null, verified))} 

     </div> 
    ) 
    } 


    render() { 
    if (!this.props.verified || !this.props.areas) { 
     return <div>Loading Areas...</div> 
    } 
    return(
     <div> 
      {this.props.areas.map(this.renderContinents.bind(this, this.props.verified))} 
     </div> 
    ); 
    } 
} 

function mapDispatchToProps(dispatch){ 
    return bindActionCreators({ fetchAllAreas, checkArea}, dispatch); 
} 


function mapStateToProps(state) { 
    return { areas: state.areas.all, 
      verified:state.areas.verified 
    }; 
} 


export default connect(mapStateToProps, mapDispatchToProps)(AreasComponent); 

私の他の問題はonChange(e)機能です。それはグローバルなので、チェックボックスをクリックすると動作しますが、onChangeをクリックすると、パラメータを取り込み、アクションcheckAreaを送出できるようにしたいと思います。これは、バインドされている必要があります。パラメータとして供給される。私はこれを試しました:

{this.props.areas.map(this.renderContinents.bind(this, this.props.verified, this.props.checkArea))} 

しかし、それは空白の結果を返します。 map()パラメータに関数を送ることは可能ですか?また、renderCountry/renderCityをパラメータで動作させる方法がありますか?

+0

これはhttp://stackoverflow.com/q/3800512/497418の複製です。タイトルがあなたを騙さないようにしてください。呼び出される関数を渡すのではなく、関数を直ちに呼び出すという根本的な問題の赤いニシンです。 – zzzzBov

答えて

1

renderCountry/renderCitycheckAreaアクションでonChange()を呼び出すための最も簡単な方法は、内部にそれらを置くことですAreasComponent(メンバ関数として)。したがって、彼らはonChangecheckAreaの両方にアクセスできます。

+0

私は実際にこれを3日前に考え出しましたが、これが正解とマークするのは、私がやったことに非常に似ていたからです。 – lost9123193

5

パラメータ.bindを使用すると、これらのパラメータが関数に渡される最初の値になります。 console.logの出力を見ると気になるはずです。

I.e.あなたは

var bar = foo.bind(this, x, y); 
bar(z); 

を行うときには、この順序で値を取得する:

function foo(x, y, z) {} 

あなたはあなたの関数のパラメータの順序を切り替えています

renderContinent(checked, continent) { 
    // ... 
} 

しかし、あなたができますあなたが持っているコードを保つだけです。値をrenderContinentsに渡す必要はありません。 renderContinentsなどにそれを渡すために

、どちらか.bindそれか、別の関数内で呼び出しを置く:

実際に
continent.countries.map(renderCountries.bind(null, verified)) 
// or 
continent.countries.map(country => renderCountries(country, verified)) 
+0

ありがとう!私は今Contextをレンダリングできます。簡単な質問ですが、nullパラメータは何を意味していますか?このコードをrenderCountriesに使用すると、countryとverifiedの両方がnullに見えます。コードを更新しました。ありがとうございました! – lost9123193

+0

nullは、関数がバインドされる 'this'を意味します。それは主に '.bind'が使われているものです。しかし、あなたが見ることができるように、パラメータをバインドすることもできます。 これは、好きなだけ多くのパラメータをバインドできますが、パラメータをバインドする場合は、「this」も指定する必要があります。 「これ」が何であるか気にしなければ、nullはうまくいくでしょう。 – Norbert

関連する問題