当前位置: 首页 > 面试题库 >

将属性映射到react-redux中的状态

段干英杰
2023-03-14
问题内容

我有一个用于state向用户提供数据的组件。例如<div>this.state.variableInState</div>。该组件可以调度某些方法(例如,按onClick操作)。我目前正在使用react- redux一种connect方法来映射storepropssetState派遣后有办法吗?

// actions
export function executeAction() {
  return function (dispatch, getState) {
    dispatch({
      type: 'MY_ACTION',
      payload: axios.get('/some/url')
    });
  };
}
// reducer

export default function (state = {}, action) {
  switch (action.type) {
    case 'MY_ACTION_FULFILLED':
      return {...state, myVariable: action.payload.data}
  }
}
//component
class MyComponent extends Component {
  render() {
    (<div onClick={this.props.executeAction.bind(this)}>
      {this.state.variableInState}
      </div>)
  }

  someOtherMethod() {
    // I want to operate with state here, not with props
    // that's why my div gets state from this.state instead of this.props
    this.setState({variableInState: 'someValue'})
  }
}


export default connect((state, ownProperties) => {
  return {
    // So I want to change MyComponent.state.variableInState here
    // but this method updates MyComponent props only
    // What can I do?
    variableInProps: state.myVariable
  }
}, {executeAction})(MyComponent);

问题答案:

据我了解,您要做的就是将组件的props转换为组件自己的状态。您总是可以componentWillReceiveProps像这样在组件的生命周期方法中将组件的props更改为组件的状态。

//component
class MyComponent extends Component {
  componentWillReceiveProps(newProps){
     if(newProps.variableInProps != this.props.variableInProps){
         this.setState({variableInState: newProps.variableInProps })
     }
  }
  render() {
    (<div onClick={this.props.executeAction.bind(this)}>
      {this.state.variableInState}
      </div>)
  }

  someOtherMethod() {
    // I want to operate with state here, not with props
    // that's why my div gets state from this.state instead of this.props
    this.setState({variableInState: 'someValue'})
  }
}


export default connect((state, ownProperties) => {
  return {
    // So I want to change MyComponent.state.variableInState here
    // but this method updates MyComponent props only
    // What can I do?
    variableInProps: state.myVariable
  }
}, {executeAction})(MyComponent);

componentWillReceiveProps 每当有新的道具进入组件时,方法总是由react执行,因此这是根据道具更新组件状态的正确位置。

更新:

The componentWillReceiveProps has been deprecated. So instead of this method
you can use componentDidUpdate method to do this. componentDidUpdate
method takes previous props and previous state as arguments. So you can check
for the change in props and then set the state.


componentDidUpdate(prevProps) {
  if(prevProps.variableInProps !== this.props.variableInProps){
    this.setState({variableInState: this.props.variableInProps })
  }
}


 类似资料:
  • 我需要将源类中的字段值映射到字段属性。我可以使用Mapstruct使用@mapper注释的'expression'参数来完成 有没有其他方法可以不使用“表达式”来进行映射?

  • 在我的数据库表中,我有一个列,它的内容可以是Y或N(CHECK约束)。我在Java中定义了一个String属性,在我的类中定义了一个基于String的setter。此外,为了方便起见,我添加了一个带有布尔参数的setter/getter。 那么,Hibernate基于什么属性进行映射呢?方法名称?参数名称?参数类型?在setter中使用可以吗?如果我的属性的名称与表列的名称不同,这会有什么不同吗?

  • MapStrut的新成员;对象到字符串错误: [错误] /util/LicenseMapper.java:[11,23]无法映射属性" Java . lang . object license . custom fields[]。值" to " Java . lang . string license . custom fields[]。值”。考虑声明/实现一个映射方法:“Java . lang

  • 问题内容: 考虑以下类别: 映射到“订单”表。是否可以通过外键关系将客户名属性映射到客户表? 问题答案: 是的,您可以为此使用联接映射元素。另一种选择是映射视图而不是表。但是,如果可能的话,您应该采用面向对象的方法,并绘制订单和客户之间的多对多关系。

  • 我试图映射到道具的行动,但我得到一个错误:类型错误:_this2.props.updateUsername不是一个函数 如何成功地将redux操作映射到props并成功调用函数?我在任何其他stackoverflow问题/答案中都没有看到这个错误弹出,这是一个简单的错误吗?会不会是在. index或. app中对redux的设置错误? 我试过:-不使用默认导出导入-有不同格式的mapDispatc

  • 问题内容: 我的PostgreSQL数据库(9.2)中有一个表,其中的列类型为JSON。我很难将此列映射到“ JPA2实体”字段类型。 我尝试使用String,但是当我保存实体时,出现一个异常,即它无法将字符转换为JSON。 处理JSON列时使用的正确值类型是什么? 一个简单的解决方法是定义一个文本列。 问题答案: 请参阅PgJDBC错误#265。 PostgreSQL过于严格,对数据类型转换非常