score:1
Instead of passing inProgress
prop to TestComponent
, you could maintain a local state in the TestComponent
that is used to determine whether to show progress text or a button and only pass the id
and onClickHanlder
props to TestComponent
.
When button is clicked in TestComponent
, you could set the the local state of TestComponent
to show the progress text and then call the onClickHandler
function passed as prop, passing in the id
prop and a callback function as arguments. This callback function will be called when API request is completed. This callback function is defined inside TestComponent
and only toggles the local state of the TestComponent
to hide the progress text and show the button again.
Change your TestComponent
to as shown below:
const TestComponent = ({ id, onClickHandler }) => {
const [showProgress, setShowProgress] = React.useState(false);
const toggleShowProgress = () => {
setShowProgress(showProgress => !showProgress);
};
const handleClick = () => {
setShowProgress(true);
onClickHandler(id, toggleShowProgress);
};
return (
<div>
{showProgress ? (
<p>In Progress </p>
) : (
<button onClick={handleClick}>click me</button>
)}
</div>
);
};
i have used useState
hook to maintain local state of the TestComponent
as it is a functional component but you could use the same logic in a class component as well.
Change the TestComponent
in sampleData
array to only be passed two props, id
and onClickHandler
.
{
id: 1,
name: "Product one",
action: <TestComponent id={1} onClickHandler={this.onClickHandler} />
}
and change onClickHandler
method in App
component to:
onClickHandler(id, callback) {
// make the api request, call 'callback' function when request is completed
setTimeout(() => {
callback();
}, 3000);
}
Demo
Alternatively, you could make onClickHandler
function in App
component to return a Promise
that is fulfilled when API request completes. This way you don't have to pass a callback function from TestComponent
to onClickHandler
method in App
component.
Change onClickHandler
method to:
onClickHandler(id) {
return new Promise((resolve, reject) => {
setTimeout(resolve, 3000);
});
}
and change TestComponent
to:
const TestComponent = ({ id, onClickHandler }) => {
const [showProgress, setShowProgress] = useState(false);
const toggleShowProgress = () => {
setShowProgress(showProgress => !showProgress);
};
const handleClick = () => {
setShowProgress(true);
onClickHandler(id)
.then(toggleShowProgress)
.catch(error => {
toggleShowProgress();
// handle the error
});
};
return (
<div>
{showProgress ? (
<p>In Progress </p>
) : (
<button onClick={handleClick}>click me</button>
)}
</div>
);
};
Demo
Source: stackoverflow.com
Related Query
- How to change the value of a prop of Sub Component passed as a column to React Table?
- how to change the react component css class value
- How to use the useHook in the component rather than passing the value returned from it as a prop to the components using react and typescript?
- How to re-render the component in react if useState does not change or hold the last value
- How to update the value of a prop upon a state change in React
- How can I change the state of one React component based on the value of its sibling?
- How to evaluate the passed prop to the styled component using react and typescript?
- React - How to pass props to a component passed as prop
- React warns about passed prop with value null, where the PropType for the prop is not required
- React : How to stop re-rendering component or making an API calls on router change for the same route
- In JSX | React How can the prop be passed to the button state so the button state can load a new page?
- How is an argument for a callback in React being passed from the child component to the parent component?
- How to check the value of a nested React component in a unit test with Enzyme and Jest
- How to access a prop in a layout component if my component is being passed to the layout component as a chilren prop in ReactJS?
- How to type async function passed as prop to React component
- Prop passed to child component does not change its value in child upon updating prop in parent
- How to access the latest state value in the functional component in React
- React Hooks: how to prevent re-rendering the whole component when only the props.children value changes?
- How to render component as soon as the value stored in local in React
- React component rendering even when there is no change in the state value
- How to use an enum in typescript as a default prop value for a react component
- how to change the value of the sate in react
- How to load react component based on prop value
- How to unit test React functions passed in as prop to child component with Enzyme shallow wrapper
- Styled Component - How to prevent a prop from being passed to the extended component?
- How do I fix a React TypeScript error with the return value of useHotkeys hook not matching the type of the div element ref prop
- How do you change the color of a React component using props
- How do I pass a prop to a react component yet not update that prop in the child when parent changes?
- How to update redux state using a react variable passed up to the component from a child
- How can I change the value of a property on the call of a reusable component on Reactjs?
More Query from same tag
- Getting 200 status code but response is undefined in react
- How to Control what Ace Editor Autocompleter writes to the File
- How to add import shortcuts - alias
- react-map-gl: Map Does Not Appear
- Controlled vs uncontrolled components in React
- Cordova white screen of death iOS - browserify, react, cordova
- map function with select option
- bundle.js not found while refreshing the page having ID in it's URL
- How to make clicking an anchor equivalent to clicking a details tag?
- How to import image(SVG or PNG) in React using Create React App
- ReactJS- remove HTTP header before redirect
- Error when use tabIndex in Autocomplete of MUI
- maximum call stack size exceed when try to do a counter in react js
- Boolean params in Axios get request automatically getting converted to string in server side
- createAsyncThunk and writing reducer login with redux-toolkit
- Deploy production build of ReactJS with Node express as backend
- React js rendered value of state is one step behind
- React: TypeError: dispatch is not a function
- Add to existing state within redux
- How to show only searched terms in autcomplete material Ui?
- ReactJS: ReferenceError: submitClick is not defined
- React HTML select element onChange function, trying to access 'event.target.value'
- Axios in React: GET Request works, PUT returns unauthrorized
- I need to dispatch action inside a saga by looping through an array
- tsconfig.json honoring baseUrl absolute imports only some of the time
- Reflect changes without refresh in React
- Next.js form components wont allow to be edited once I give a value
- Error handling in react router server-side
- I am having problem using http-proxy-middleware
- Transition and center the card in react