简体   繁体   English

React渲染相同的元素,它是动态组件

[英]React render same element which is dynamic component

I don't know how call this situation. 我不知道怎么称呼这种情况。 It is following 它跟随

I have a dynamic component which render a 我有一个动态组件,呈现一个

<input type="text" placeholder={this.props.reduxStateValue} />

ie

function dynamicInput() {
    var page = this.props.kindOfPage,
        rState = this.props.reduxState;
    if (page === 'foo') {
        // foo input
        // eg. rState.value === 10
        return <input type="text" placeholder={rState.value} 
                      onChange={this.handleFoo} />
    } else {
        // bar input
        // eg. rState.value === 20
        return <input type="text" placeholder={rState.value} 
                      onChange={this.handleBar} />
    }
}

The above example is very simple verion. 上面的例子是非常简单的版本。 The components has many different things for each this.props.kindOfPage . 每个this.props.kindOfPage都有许多不同的组件。

The problem is that when the page === 'foo' and I insert 100 into the <input/> and switch to page !== foo , the 100 is in the <input/> of page !== foo . 问题是,当page === 'foo'并且我将100插入<input/>并切换到page !== foo100位于page !== foo<input/> page !== foo

ie If I call the <input/> of page === 'foo' case as foo input and another as bar input , 即如果我将<input/> page === 'foo'情况称为foo input而另一个作为bar input

  1. insert 100 into foo input 100插入foo input
  2. switch the page to page !== foo then bar input is rendered 将页面切换到page !== foo然后呈现bar input
  3. the 100 is in bar input 100bar input

But the real value of each <input> has its own value. 但每个<input>的实际价值都有自己的价值。 ie

  1. The foo input : foo input

value : 100 , placeholder : 10 , shown value : 100 值: 100 ,占位符: 10 ,显示值: 100

  1. The bar input : bar input

value : '' , placeholder : 20 , shown value : 100 值: '' ,占位符: 20 ,显示值: 100

What I want to do is that the bar input show its placeholder 20 since we do not insert the value 100 in bar input . 我想要做的是bar input显示其占位符20因为我们没有在bar input插入值100

I think this problem is occurred because of VirtualDOM of react. 我认为这个问题是因为VirtualDOM的反应而发生的。 The renderer just change its DOM when there is a different between each elements. 当每个元素之间存在不同时,渲染器只是更改其DOM。 Hence, whenever I change the attributes, the element is not changed and the value of input element also not changed. 因此,每当我更改属性时,元素都不会更改,并且input元素的value也不会更改。

But, I want to separate both and show different values. 但是,我想将两者分开并显示不同的值。 Is there any idea? 有什么想法吗?

Perhaps you could approach this problem slightly differently by decoupling dynamic event binding from your component's render method to deal with this characteristic of React's rendering and DOM diffing. 也许您可以通过将动态事件绑定与组件的render方法分离来处理React渲染和DOM diffing的这一特性,从而稍微改变这个问题。

Rather than conditionally render components with different event handlers as you currently are, why not opt for a more efficient render implementation (ie without conditional branching) by using a common event handler that dynamically invokes the appropriate event based on your kindOfPage state? 而不是像现在一样有条件地使用不同的事件处理程序呈现组件,为什么不通过使用基于kindOfPage状态动态调用适当事件的公共事件处理程序来选择更有效的呈现实现(即没有条件分支)?

So for instance, you could follow this pattern (pseudo code): 因此,例如,您可以遵循此模式(伪代码):

function handleGeneric(event) {

    var page = this.props.kindOfPage;

    // Introduce dynamic branching in "generic" event handler, like so
    switch(page) {
      case 'foo':{
        this.handleFoo(event)
        break;
      }
      default:{
        this.handleBar(event)
        break;
      }
    }
}

function dynamicInput() {
    var rState = this.props.reduxState;

    // Simplyify your render method in this way, and delegate conditional
    // branching to event handler    
    return <input type="text" placeholder={rState.value} 
                  onChange={ event => this.handleGeneric(event) } />
}

Assuming I am right, I think you can simply add a key attribute to the input to fix this problem. 假设我是对的,我想你可以简单地在输入中添加一个键属性来解决这个问题。 like this: 像这样:

renderInput () {
        var page = this.state.kindOfPage
        var rState = this.state.reduxState;
        if (page === 'foo') {
            // foo input
            // eg. rState.value === 10
            return <input type="string" placeholder={rState.value1} key={"input1"}
                          onChange={this.handleBar} />
        } else {
            // bar input
            // eg. rState.value === 20
            console.log('test')
            return <input type="string" placeholder={rState.value} key={"input2"}
                          onChange={this.handleFoo} />
        }
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM