2017-06-19 54 views
0

我想在簡單的Input redux表單字段中進行簡單的數據注入。 讓我解釋一下:在輸入字段上使用「更改」監聽器

我有一個大的形式,當用戶在「姓氏」輸入內寫入時,我只是想將值注入第二個字段作爲「LastNameFather」。

很簡單伊莫

我做了一個formvalueSelector,並獲得第一個輸入值。

const selecor = formValueSelector('MyForm') 

const mapStateToProps = (state,props) => { 

    const name = selector(state,"nameOne") 

    return { 
    name : name 
    }} 

而我使用componentWillReceiveProps來處理寫作,並使用道具更改注入我的價值。

componentWillReceiveProps = (props) => { 
    props.name != null ? props.change("name2",props.name) : null 
} 

現在,當我在我的第一個字段中寫入時,有無限的重新渲染。正如你所看到的,我必須在更改第二個字段之前測試props.name!= null。

無論如何,我錯過了什麼?

感謝

+0

你是否將調度綁定到'props.change'? –

+0

是的,我可以看到我的動作正確發送。 我不知道什麼錯。 – GreGGus

+1

我不會走你自己的路@GreGGus,我更喜歡在'lastname'的'Field'上做'onBlur'監聽器,並且我會去'()=> props.change(...)'。 –

回答

1

GreGGus先生,根據陣營的js官方文檔「請注意,可能做出反應調用這個方法(componentWillReceiveProps),即使道具都沒有改變,所以一定要確保,如果比較當前和未來值你只想處理更改,這可能在父組件重新呈現時發生。「 你可以試試。