I am creating a simple application having state as following
const [questions, setquestions] = useState([ {
id: '1',
author: 'sarahedo',
optionOne: {
votes: ['sarahedo'],
text: 'have horrible short term memory',
},
optionTwo: {
votes: [],
text: 'have horrible long term memory'
}
},
{
id: '2',
author: 'johndoe',
optionOne: {
votes: [],
text: 'become a superhero',
},
optionTwo: {
votes: ['johndoe', 'sarahedo'],
text: 'become a supervillian'
}
},]
This function updates the state (you don't have the need to understand the complete functionality)
const voteHandler = (id, selectedOption, voter) => {
questions_copy.filter(singleObject =>
{if(singleObject.id === id){
singleObject["optionOne"]["text"] === selectedOption ? singleObject["optionOne"]["votes"].push(voter) : singleObject["optionTwo"]["votes"].push(voter)
}
}
)
setquestions(questions_copy)
}
I have sent this function using props to its child component and when this function is called it gives me this error
Warning: Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in a useEffect cleanup function.
As far as calling in the child component (another router) is concerned, I am calling it in this way
const onSubmitHandler = (e) => {
e.preventDefault()
props.history.location.data.voteHandler(props.location.state.id, selectedOption, props.location.state.dummyLogin)
props.history.push("/")
}
I am simply updating the state, I don't know why it is behaving so