React Native + Redux:为什么切换到true后切换会立即返回false?

Jo *_* Ko 7 javascript reactjs react-jsx react-native redux

在IOS阵营母语+终极版,我使用以下交换机组分(https://facebook.github.io/react-native/docs/switch.html).它首先设置为关闭,但是当打开时,它会立即自动关闭.可能是什么问题?

这是我的设置:

<Switch
  onValueChange={this._handleSwitch}
  value={switch.currentValue}
/>
Run Code Online (Sandbox Code Playgroud)

触发的动作是:

  _handleSwitch(value) {
    this.props.actions.triggerSwitch(value)
  }
Run Code Online (Sandbox Code Playgroud)

行动是:

export function triggerSwitch(value) {
  return {
    type: TRIGGER_SWITCH,
    currentValue: value
  }
}
Run Code Online (Sandbox Code Playgroud)

在减速机中:

const initialState = {
  currentValue: false
}

function switchReducer(switch = initialState, action) {
  switch(action.type) {
    case TRIGGER_SWITCH:
      return {
        currentValue: action.currentValue
      }

     default:
       return switch
  }
}

export default switchReducer
Run Code Online (Sandbox Code Playgroud)

谢谢!

onm*_*133 5

不是因为redux,为了Switch工作,我们需要显式设置valuefrom一个状态,否则false立即设置回

<Switch
  value={this.state.hasRead}
  onValueChange={(value) => {
    this.setState({
      hasRead: value
    })
}} />
Run Code Online (Sandbox Code Playgroud)


dam*_*net 0

你的_handleSwitch函数绑定正确了吗?发货好吗this.props.actions.triggerSwitch(value)

使用redux-logger检查每个阶段的状态和操作,并确保您的组件在切换后收到正确的值。