【问题标题】:How do I use redux when a prop function is changing the state?当 prop 函数改变状态时如何使用 redux?
【发布时间】:2016-07-25 23:50:24
【问题描述】:

我正试图围绕在传递给组件的 prop 应该用于更改应用程序状态的情况下如何使用 redux。

我有一个工作示例here

let Input = ({handleChange}) => (
  <input type="text" onChange={handleChange('mySpecialInput')} />
)

let Text = ({message, color}) => (
  <span style={{color}}>{message}</span>
)

let App = ({message, color, handleChange}) => (
  <div>
    <Text message={message} color={color} /> <br />
    <Input handleChange={handleChange} />
  </div>
)

class ConnectedApp extends React.Component {
  constructor(props) {
    super(props)
    this.state = {
      color: 'orange',
      message: 'Hello World'
    }
    this.handleChange = this.handleChange.bind(this)
  }
  handleChange(id) {
    return (event) => {
      console.log(id)
      if (id === 'mySpecialInput') {
        this.setState({'color': event.target.value})
      }
    }
  }
  render() {
    return (
      <App 
        message={this.state.message} 
        color={this.state.color} 
        handleChange={this.handleChange} />
    )
  }
}

ReactDOM.render(
  <ConnectedApp/>,
  document.getElementById('react_example')
);

如何使用 redux 来处理像这样简单的事情?

【问题讨论】:

  • 第二个例子不行吗?我不太明白你的问题。
  • @mjohnsonengr 第二个例子直接取自simplest-redux-example,刚刚更新,我想我明白了,但仍然没有渲染。
  • 可能没有渲染,因为 App 没有收到 message 属性。
  • 当您执行mapStateToProps 时,您实际上是在声明一组 props,react-redux 将传递给您的包装组件,例如应用程序。您的 mapStateToProps 仅定义一个颜色。如果您将其他道具传递给 ConnectedApp,它们将被转发给 App
  • 名称修改。 changeColor 定义了两次。

标签: javascript reactjs state redux


【解决方案1】:

您可以简单地单独连接每个组件,而不是在顶级组件上使用 connect() 并在层次结构树中向下传递道具。

这样会更方便。
See a working example!

【讨论】:

    【解决方案2】:

    这是上面使用 redux 构建的代码!

    Working Example

    let {createStore} = Redux
    let {connect, Provider} = ReactRedux
    
    // React component
    let Input = ({handleChange}) => (
      <input type="text" onChange={handleChange('mySpecialInput')} />
    )
    
    let Text = ({message, color}) => (
      <span style={{color}}>{message}</span>
    )
    
    let App = ({message, color, handleChange}) => (
      <div>
        <Text message={message} color={color} /> <br />
        <Input handleChange={handleChange} />
      </div>
    )
    
    // Action
    
    const CHANGE_COLOR = 'CHANGE_COLOR'
    
    function changeColorAction(color) {
      return {
        type: CHANGE_COLOR,
        color
      }
    }
    
    // Reducer
    function reducer(state = {color: "#ffa500"}, action) {
      let count = state.count
      switch (action.type) {
        case CHANGE_COLOR:
          return { color: action.color }
        default:
          return state
      }
    }
    
    // Store
    let store = createStore(reducer)
    
    // Map Redux state to component props
    function mapStateToProps(state) {
      return {
        color: state.color,
        message: "Hello World"
      }
    }
    
    function changeColorDispatcher (dispatch) {
      return (id) => {
        return (event) => {
          if (id === 'mySpecialInput') {
            return dispatch(changeColorAction(event.target.value))
          }
        }
      }
    }
    
    // Map Redux actions to component props
    function mapDispatchToProps(dispatch) {
      return {
        handleChange: changeColorDispatcher(dispatch)
      }
    }
    
    // Connected Component
    let ConnectedApp = connect(
      mapStateToProps,
      mapDispatchToProps
    )(App)
    
    ReactDOM.render(
      <Provider store={store}>
        <ConnectedApp/>
      </Provider>,
      document.getElementById('react_example')
    );
    

    【讨论】:

    • 你为什么要费心将动作创建者作为道具向下传递?这似乎是错误的。
    • @whitep4nther 哪个变量是动作创建者?我的选择是什么?
    • 我发布了一个答案,该答案更符合“前进之路”;)
    猜你喜欢
    • 2021-12-30
    • 1970-01-01
    • 2021-08-11
    • 1970-01-01
    • 2019-02-03
    • 2020-01-20
    • 2020-09-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多