我創建了一個非常簡單的yes/no組件映射到與真正/錯誤的值與redux形式。 單擊「否」時,商店中的值會更新,但組件不會更新。 只有點擊是後,組件纔會更新點擊編號還原窗體組件不反映存儲狀態
我在這裏做錯了什麼?爲什麼商店中的狀態沒有反映在組件中?
請注意,對於我的用例來說非常重要的是,最初沒有單擊按鈕。
沙箱:
https://codesandbox.io/s/r06VKjB4K
import React from 'react';
import { Field, reduxForm } from 'redux-form';
const buttonStyle = {
width: '50px',
display: 'inline-block',
border: '1px solid green',
margin: '5px',
padding: '5px',
cursor: 'pointer',
textAlign: 'center',
};
const ButtonBar = ({ options, input }) =>
<div style={{ display: 'block' }}>
{options.map(x =>
<div
onClick={() => {
input.onChange(x.value);
}}
style={{
...buttonStyle,
...{
backgroundColor: input.value === x.value ? x.selected : 'white',
},
}}
>
{x.displayName}
</div>,
)}
</div>;
const SimpleForm = props => {
return (
<form>
<div style={{ display: 'inline-block', border: '1px solid grey' }}>
<Field
name="myButton"
component={ButtonBar}
options={[
{
value: true,
displayName: 'Yes',
selected: 'green',
},
{
value: false,
displayName: 'No',
selected: 'red',
},
]}
/>
</div>
</form>
);
};
export default reduxForm({
form: 'simple', // a unique identifier for this form
})(SimpleForm);
你能不能保持像這樣 input.value == x.value ? – VivekN
@VivekN不,我在問題中指出爲什麼不是 –
確實與「真/假」值有關。如果用「a」和「b」代替它,它會按預期工作。我對redux形式不是很熟悉,但文檔可能會解釋這一點。 – enapupe