简体   繁体   English

动态反应 setState - 如何从道具中设置状态属性和值

[英]React setState dynamically - how to setState property and value from props

I'm trying to create a single handler to update the state.我正在尝试创建一个处理程序来更新 state。 For now i have many handlers - one for every component.现在我有很多处理程序 - 每个组件一个。 What i'm trying to do is create switch function and one handler to handle all state changes.我要做的是创建开关 function 和一个处理程序来处理所有 state 更改。

Here's the switch function, for now it has only one case:这里是开关 function,目前只有一种情况:

function liftingUpStateHandler(type, e, state){
    switch(type){
        case 'imageClickHandler':
            let array = state.bottomGalleryItems;
            const index = array.findIndex((object) => object.cardId === e);
            window.scrollTo({ top: 0, behavior: 'smooth' });
            return  ['items', [state.bottomGalleryItems[index]]]
        default:
            return null;
    }
}
export default liftingUpStateHandler;

And i'm trying to create one handler in main app.js file:我正在尝试在主 app.js 文件中创建一个处理程序:

  stateHandler = (type, e) =>{
    const result = liftingUpStateHandler(type, e);
    this.setState({result[1]: result[2]})
  }

If this would work i can just add cases to liftingUpStateHandler and update state with only one handler.如果这可行,我可以将案例添加到 liftUpStateHandler 并仅使用一个处理程序更新 state。

(one to rule them all):) (一个统治他们):)

What i'm dealing now is a lot of functions like this:我现在处理的是很多这样的功能:

loaderScreenHandler = (e) => {
    this.setState({loader: e})
  }

  modalCloseHandler = () =>{
    this.setState({noexifdatafilenames: []})
  }

  markerFlyerTo = (e) => {
    const index = (markerFlyerHandler(e, this.state));
    this.setState({ activeCard: index });
  }

  deleteItem = (e) => {
    const result = (deleteItemHandler(e, this.state));
    this.setState({ items: result[0]});
    this.setState({ activeCard: 0 });
  };

  changeActiveCardRight = () => {
    const result = (changeActiveCardRightHandler(this.state, 'right'));
    this.setState({ activeCard: result });
  }

  changeActiveCardLeft = () => {
    const result = (changeActiveCardRightHandler(this.state));
    this.setState({ activeCard: result});
  }

  closeHandler = () => {
    this.setState({ fullScreen: false})
  }

... But i'm getting an error: ...但我收到一个错误:

Failed to compile

./src/App.js
SyntaxError: C:\Users\Sławek\Desktop\dev\geolocapp\src\App.js: Unexpected token, expected "," (63:25)

  61 |   stateHandler = (type, e) =>{
  62 |     const result = liftingUpStateHandler(type, e);
> 63 |     this.setState({result[1]: result[2]})
     |                          ^
  64 |   }
  65 |
  66 |

Is this even possible?这甚至可能吗? Or i need to take different approach like REDUX?或者我需要采取不同的方法,比如 REDUX?

this.setState({result[1]: result[2]})

To do a computed object property , you need to use square brackets around the key:计算 object 属性,您需要在键周围使用方括号:

this.setState({ [result[1]]: result[2] })

Also, i think you meant to use result[0] and result[1] , so it would be:另外,我认为您打算使用result[0]result[1] ,所以它是:

this.setState({ [result[0]]: result[1] })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM