2016-06-16 67 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