【问题标题】:how to create abstract state value on react如何在反应上创建抽象状态值
【发布时间】:2019-12-11 03:46:01
【问题描述】:

我想要一个抽象的默认状态值来对映射的组件做出反应。逻辑是这样的 1.参数的json 2. 将json参数映射到对象 3.根据item.value设置对象的默认值

不知道怎么做3号

这是代码,

    function parameterItem(item) {
        if(item.type === 'date') {
            return <Input type="date" name={ item.name } defaultValue={this.state.[item.value]} id={ item.name} form="form-filter" />
        }
    }

    if(this.props.filter) {
        parameters = this.props.filter.map((item, id) => {
            if (Object.keys(item).length > 0) {
                return <Col xs='12' key={id} hidden={ !item.visible }>
                    <Row className="form-spacing-top">
                        <Col xs='3'>
                            <p>{ item.caption }</p>
                        </Col>
                        <Col xs='9'>
                            { parameterItem(item) }
                        </Col>
                    </Row>
                </Col>
            } else return null
        });
    }

【问题讨论】:

  • 你的意思是取决于 item.value 还是 item.type ?
  • 取决于从 parameter.json 设置的 item.value

标签: reactjs state abstract


【解决方案1】:

你的意思是说你在某个文件的某处有这样的映射:

ma​​pping_constants.js:

default_value_mappings = {
    "value1": "Some Value1", 
    ...
    ...
    "valueN":"Some Value N"
}


可以映射为:

componentDidMount(){
    this.setState({"defaults":default_value_mappings})

}
function parameterItem(item) {
    if(item.type === 'date') {
            return <Input type="date" name={ item.name } defaultValue={this.state.defaults[item.value]} id={ item.name} form="form-filter" />
    }
}

我的问题是否正确?

【讨论】:

  • 那么我有没有回答您关于您仍在寻找其他机制的问题?
  • 我把状态改成了变量,然后把变量赋值为item.name as function parameterItem(item) { if(item.type === 'date') { return } } 不过我明白了。我无法获取 [item.name] 的值,而只能获取名称的字符串值
猜你喜欢
  • 2015-03-10
  • 1970-01-01
  • 1970-01-01
  • 2020-09-25
  • 2020-11-23
  • 1970-01-01
  • 1970-01-01
  • 2019-11-05
  • 1970-01-01
相关资源
最近更新 更多