【问题标题】:how to get value of column in react如何在反应中获取列的值
【发布时间】:2017-08-04 09:38:19
【问题描述】:

我有这样的表列,它的值来自我的减速器。同一列有递增/递减按钮。因此,当使用单击按钮时,我必须从该 <td> 中获取价值并触发我的操作。我正在搜索here,但它用于搜索具有事件onChange 的输入文本。请任何人告诉我如何做到这一点?

这是我的<td> 专栏:

<td>
    {this.props.prog}%
    <button type="button" className="btn btn-danger">
        <span className="glyphicon glyphicon-arrow-up" />
    </button>
    <button type="button" className="btn btn-danger">
        <span className="glyphicon glyphicon-arrow-down" />
    </button>
</td>

编辑 1:

我已经尝试了一些基本上我的 onClick 正确触发但我无法传递值

<td>
                        <button type="button" className="btn btn-danger" onClick={this.props.resetGrowth}>Reset</button>
                        {this.props.prog}%
                        <button type="button" className="btn btn-danger" onClick = {(event) => this.props.updated(this.props.prog) }>
                        <span className="glyphicon glyphicon-arrow-up"/>
                        </button>
                        <button type="button" className="btn btn-danger">
                        <span className="glyphicon glyphicon-arrow-down"/>
                        </button>
                    </td>

它会将这个组件传递给它

<Table updated={value => this.props.udated(value)}/>

这是我的容器,它触发了价值

 <VolumeComponent 
              udated = {(value) => updated(value)}/>

const mapDispatchToProps= (dispatch) => (
 bindActionCreators({updated},dispatch)
)

【问题讨论】:

    标签: javascript html reactjs react-redux


    【解决方案1】:

    由于您使用的是 reducer,我猜您使用的是 redux。如果{this.props.prog} 是您的值,您可以在传递给增加/减少按钮的 OnClick 函数中使用它。例如:

    handleDecrease(){
        const newValue = this.props.prog + 1;
        this.props.updateValue(newValue)
    }
    
    handleIncrease(){
        const newValue = this.props.prog - 1;
        this.props.updateValue(newValue)
    }
    
    render(){
        return (
            ...
            <button onClick={this.handleIncrease}/>
        );
    }
    

    【讨论】:

    • 是的,它是 redux,你能添加一个例子吗?因为我得到的价值来自商店
    • 是的,但我没有在这里维护任何状态。加载时componentdidmount 触发了一个给我价值的动作,我从mapStateToProps获取值
    • 当你使用 redux 时,如果没有必要,你应该尽量避免使用组件状态。相反,您应该添加一个 updateValue(或您希望如何调用它)操作,它会更新商店。
    • componenetdidmount 写了不在组件中的容器,我没有任何状态
    • 你需要的东西会这样添加吗?
    猜你喜欢
    • 2021-08-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-30
    • 2022-09-23
    • 2020-05-23
    • 1970-01-01
    • 1970-01-01
    • 2019-04-18
    相关资源
    最近更新 更多