score:1
I finally solved this by making my component a class component so I could use componentDidUpdate
componentDidUpdate(prevProps) {
if (prevProps.currentLineIndex !== this.props.currentLineIndex) {
this.scrollToCurrentLine(this.props.currentLineIndex);
}
}
handlePlaybackEnd = () => this.props.setCurrentLineIndex(this.props.currentLineIndex + 1);
2020 UPDATE
Hooks made it a lot simpler, no need for a class component, just use an effect:
const scrollToCurrentLine = useCallback(() => {
const currentLineEl = document.getElementById(currentLineIndex);
currentLineEl.scrollIntoView({ block: 'start', behaviour: 'smooth' });
}, [currentLineIndex]);
useEffect(scrollToCurrentLine, [scrollToCurrentLine]);
score:0
I solved a similar problem by creating a little npm module. It allows you to subscribe to and listen for redux actions and executes the provided callback function as soon as the state change is complete. Usage is as follows. In your componentWillMount or componentDidMount hook:
subscribeToWatcher(this,[
{
action:"SOME_ACTION",
callback:()=>{
console.log("Callback Working");
},
onStateChange:true
},
]);
Detailed documentation can be found at this link
score:1
One solution can be to define setCurrentLineIndex
such that it receives a callback.
//assuming the parent container is a class
setCurrentLineIndex = (index, cb)=>{
//write the logic here then execute the callback
cb()
}
// Then in your child component, you could do something like this
setCurrentLineIndex(nextIndex, scrollToCurrentLine)
Source: stackoverflow.com
Related Query
- How to wait for a Redux action to change state from a React component
- how and when to call a react component methods after state change from redux
- How to wait for redux state change and then render page in React
- How can I initialize Redux state for React component on its creation?
- How to make React component wait for redux store variables to get set?
- Wait for change of prop from parent component after changing it from a child in React
- How to pass input values from a child Form component to the state of its parent component for submission with react hooks?
- How to update redux state using a react variable passed up to the component from a child
- How do you change state of a react component from a different component?
- How do I stop state store data from accumulating in a redux component every time I navigate to it using react router
- How do I tell React component to change its state when an event is triggered from third party lib?
- How to change only a part of the state from child component react hooks
- how does react props merge state passed from parent component and redux store
- React Redux wait for state change
- How to set state at the server for data from API using react redux
- How to dispatch an action to change the inner state in a react component
- react wait for component to get state from store then render child
- How to run a timer in React component for 60 seconds and call a Redux action every 5 seconds
- How can I wait for state to change in Redux Saga before using google tag manager?
- How to change state of modal from a different component in react
- React - how to change state within component from another component?
- react redux how to trigger container function with action coming from non react component
- How to refresh onscreen data from state change in react and redux
- How can I change css for the React code depending on the Redux state (true or false)
- how to change state of a react component from another component
- How to stub async action in componentDidMount for react redux connected component test using Jest
- React js change child component's state from parent component
- React & Jest, how to test changing state and checking for another component
- How to dispatch Redux action from stateless component when route is loaded?
- How to set React component state and props from browser
More Query from same tag
- setState() of an object within an object with a variable
- I need to set and send value of slider after onChange
- Component definition is missing display name - React.js
- How can I achieve this sequential fade-in effect?
- Writing CSS in JS code to remove arrow buttons from TextField
- Build library and include CSS files but not test files
- How to detect Esc Key Press in React and how to handle it
- React Js application on page refresh/reload gives error
- Tab.Screen does not re-render
- Getting according to auto scroll to content in React using Semantic-UI React components
- Test whether React component has rendered
- setState did not accept dynamic value in react?
- How to get transitions to work each time an element is clicked
- How to get HTML tags from object value?
- Api call fills one array but not the other?
- Reactjs URL.createObjectURL is not a function
- Check if an object is present in a nested array reactjs
- Why isn't my component rerendered when I change the state in componentDidMount?
- How do I build a generic typescript custom hook?
- React form validation error - Invalid attempt to spread non-iterable instance
- A string ref, "grid", has been found within a strict mode tree
- Creating test payment gateway with react and stripe
- React Router Navigate Issue in Semantic Ui React
- useEffect only if state changed
- how to write script inside jsx?
- Avoid recalculating variable on every render in React
- useEffect is not firing consistently
- In React, how do I instantiate a class without a constructor in my unit test?
- Can react hooks provide hooks?
- react - material-ui appbar icon touch event doesn't fire