鍍金池/ 問答/HTML/ 請問:react如何給通過屬性傳遞的組件 傳屬性

請問:react如何給通過屬性傳遞的組件 傳屬性

render() {
    const Item =this.props.renderItem //這是傳遞過來的組件
    
    return (
      <View
        style={{ backgroundColor: 'orange', flex: 1, position: 'relative' }}
      >
        <View
          {...this._panResponder.panHandlers}
          style={{ backgroundColor: 'red' }}  //現(xiàn)在是通過父組件
        >
          {this.props.renderItem} // 我想直接向這里面?zhèn)鲗傩?        </View>

      </View>
    )
  }
回答
編輯回答
櫻花霓

用render props實現(xiàn)

官方文檔
//父組件
render() {
    const props = Object.assign(
      {...this._panResponder.panHandlers},
      {style :{ backgroundColor: 'red'}}
    )
    return (
      <View
        style={{ backgroundColor: 'orange'... }}
      >
        {this.props.renderItem(props)}
      </View>
    )
  }
子組件
<Gesture
    renderItem={
        props => (
            <Image
                source={{ uri: 'https:xxx.jpg' }}
                {...props}
            />
        )
    }
/>
2018年8月16日 03:50
編輯回答
刮刮樂

把父組件中的 props.renderItem 換成 props.children 來注入子組件

<Father fatherProps={}><Child childProps={/* 這里傳入你想直接傳給子組件的 props */}></Father>
2017年5月5日 14:03