how can I reuse the function to update different parts of the state of a React component?

a1olegs
class App extends Component {      
constructor(props) {
super(props);
this.state = {
  Foo: [],
  Bar: []
};
this.xfetch('Alice',  this.state.Foo);    
}

xfetch = (param, target)=> {
   const res = getRes(param);//db request async
   //onCompleted: () => { 
   this.setState(({ target}) => {
      const newArr = [...target, res ];

      return {
        target: newArr
      };
    });
}

addBar = input => {
this.xfetch(input,  this.state.Bar);

};

the code above doesn't work, and I don't want to write multiple versions of xfetch() for different parts of the state

*first part of the state I want to set in the constructor

Red Baron

from what it sounds like, you are wanting to update state dynamically?

try this:

xfetch = async (param, target) => {
   const res = await getRes(param)
   this.setState({ [target]: [...target, res ] })
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

how do i reuse a react component but with different colours?

Why I can't get update the state of my react component?

React: How can i run a function from another Component with state Variables

Using React, how can I update state from inside a custom function?

How to update a single property in a React state (function component)

React hooks: Can't update state inside function component

How can I initialize Redux state for React component on its creation?

React: how can I force state to update in a functional component?

How can I prevent a React state update on an unmounted component in my integration testing?

How can I update my React component state?

How can I update a single React Component from multiple different components?

How can I access this.state of React component inside of a filter function?

How can I set the state of a component on video end in react?

How can I reuse a function for many different ID values?

How can I set redux state in a function (not component)

How can i filter an state array in functional component in react native

React - How can I set my parent state using child component, using function structures (not classes)

How can I reset a component to it's initial state in react

How do I avoid "Can't perform a React state update on an unmounted component" error on my application?

React Native - Can't perform a React state update on an unmounted component, useEffect cleanup function

How can I render different component using onClick() function in React?

How can I reuse a component with different props in React?

How can I export state to parent functional component in React?

How can I change state of current map item in react component

How do i access a function in a component with a button in a different component in React?

React how can I change a state based on a different state, in the same function

How to reuse component with different tags using react

How can I update state in a component when the same state is passed as props in another component?

why React update state function doesn't update state if i callback a function from a parent component?