2016-06-16 145 views
0

當道具傳遞給在Redux的特定組件所以我有這個組件中,我使用地圖功能調用另一個組件:如何迭代

class configstepper extends React.Component { 
... 
//in render 
if(this.props.selected_devices){ 
    return(
      this.props.selected_devices.map((row, index) => (
       <DeviceProps key={index} row={row} index={index} /> 

      )))} 
     ...} 

現在我DeviceProps組件中獲取數據,因此具有道具來呈現 我的問題是我想要的道具要具體到每個DeviceProps不是所有的人:

class deviceProperties extends Component { 
... 

//in render 
<RaisedButton style={styles.Button}label="Read From Device" primary={true} onClick={this.handleRead.bind(this)}/> 
<TextField id={this.props.index} value={this.props.read_data} readOnly/> 
... 
//handleRead where i trigger the action 
    handleRead(){ 

    this.props.read_device() 
} 

//mapstate to props 
function mapStateToProps(state){ 
return { 
    read_data:state.config.test}} 

connect(mapStateToProps,actions)(deviceProperties) 

數據是按鍵點擊獲取當我使所有DeviceProps組件獲得相同的數據,同時我只想在點擊的組件上獲得它ENT。 我想不出圍繞這一

+0

請提高您的描述。 DeviceProps與'deviceProperties'相同嗎?如何應用mapStateToProps?什麼?分享DeviceProps的全部實施。 –

+0

是deviceProperties是DeviceProps我的不好 我修改了我的代碼我希望它更清楚 – safouman

+0

嘿,你是否得到了答案?我遇到了同樣的問題 –

回答

0

mapStateToProps功能的工作接受第二個參數是你傳遞到從父組件中的道具,你可以用這些來抓取你的狀態的切片特定於您的每個DeviceProps實例。同樣,mapDispatchToProps也接受道具作爲第二個參數,允許您創建特定於每個DeviceProps實例的操作。

例如,假設你在道具均及格稱爲deviceId到您的DeviceProps組件,與deviceId是針對特定設備的唯一標識符:

<DeviceProps key={index} row={row} index={index} deviceId={deviceId} /> 

然後,你可以在你的地圖狀態下使用這個唯一的標識符道具功能如下:

class DeviceProps extends Component { 
    render() { 
     ... 
    } 
} 

function mapStateToProps(state, ownProps){ 
    return { 
    device: state.devices[ownProps.deviceId] 
    } 
} 

function mapActionsToProps(dispatch, ownProps) { 
    return bindActionCreators({ 
    readDeviceData:() => DeviceActions.readDeviceData(ownProps.deviceId) 
    }, dispatch) 
} 

connect(mapStateToProps, mapActionsToProps)(DeviceProps) 
+0

問題出在textfield的值,當我分配給它的動作產生的道具deviceprops實例中的所有文本域將具有相同的值 – safouman

+0

聽起來像你可能需要構建你的REDX存儲,以便您可以存儲每臺設備的數據。如果他們都共享相同的商店屬性,那麼他們最終會顯示相同的數據。 – ctrlplusb

+0

關於如何做到這一點的任何想法? – safouman