How do i load all images before rendering the page? - reactjs

So i have a preloader that works, i just have some problems with my image component itself.
My image component below in my theory should after the first render get all of my image tags used in my project.
Then when the image event onLoad is fired, it should update a state which is didLoad and set it to true. When the page is rerendered again because of the updated state, it should check if didLoad is true, if it's true it should execute the useEffect inside the condition once and add 1 to the amount loaded.
And then i check if images is higher than 0, and images is equal to images loaded then remove preloader.
Obviously something is wrong since i can't get it to work, it needs to work with all images not one at a time because it's not a lazyload for the image itself, but the preloader for the whole page.
Ignore preload: false it's for when the preloader is removed, then it should play page animations.
/** #jsx jsx */
import React, { useContext, useEffect, useState } from "react";
import { css, jsx } from "#emotion/core";
import preloadContext from "../../context/preload.context";
const Img = ({ src, alt }) => {
const [didLoad, setLoad] = useState(false);
const [preload, setPreload] = useContext(preloadContext);
const imgStyle = css`
width: 100%;
height: 100%;
display: block;
object-fit: cover;
`;
useEffect(() => {
setPreload({
amount: preload.amount++,
preload: preload.preload,
isLoaded: preload.isLoaded,
});
}, []);
if (didLoad === true) {
useEffect(() => {
setPreload({
amount: preload.amount,
preload: preload.preload,
isLoaded: preload.isLoaded++,
});
}, []);
}
return (
<img
src={src}
onLoad={(e) => {
setLoad(true);
}}
css={imgStyle}
alt={alt}
onDragStart={(e) => e.preventDefault()}
/>
);
};
export default Img;

The answer is You can and You cannot.
We can : if you're trying to hold rendering of a certain component until an <img /> (Outside of this component is loaded)
We cannot : If you're trying to track loading of <img /> which is INSIDE of a not rendered component. In this case Image won't load until we render it's parent component.
For the first option here's how you can do it:
constructor(props) {
super(props);
this.state = { loaded: 0 };
this.handleImageLoaded = this.handleImageLoaded.bind(this);
this.image = React.createRef();
}
componentDidMount() {
const img = this.image.current;
if (img && img.complete) {
this.handleImageLoaded(10); // 100 / Number of images to track
}
}
handleImageLoaded(loadIncrement) {
if (!this.state.loaded)
this.setState({ loaded: loadIncrement });
}
render() {
return (
<div>
<img src='image.jpg' ref={this.image} onLoad={this.handleImageLoaded} />
{this.state.loaded >= 100 && <MyComponent />}
</div>
);
}
As mentioned in the comment you must divide 100 to number of images you wish to track and then one the image is loaded it will add the loading percentage to the parent state. One the loaded state reach 100 or above it will render <MyComponent />

Related

How to update state just after rendering

I have the following component:
import React from 'react';
import './styles.css';
import ToolTip from '../../Common/components/ToolTip/ToolTip';
export default class RouteTitleTooltipComponent extends React.Component {
constructor(props) {
super(props);
this.titleParagraphRef = React.createRef();
this._tooltipTimer = null;
this.state = { shouldPopupBeEnabled: false, isTooltipShown: false };
this._showTooltip = this._showTooltip.bind(this);
this._hideTooltip = this._hideTooltip.bind(this);
}
componentDidMount() {
const { scrollWidth, clientWidth } = this.titleParagraphRef.current;
const shouldPopupBeEnabled = scrollWidth > clientWidth;
this.setState({ shouldPopupBeEnabled });
}
_showTooltip() {
this._tooltipTimer = setTimeout(
() => {
this.setState({ isTooltipShown: true });
}, 1000,
);
}
_hideTooltip() {
clearTimeout(this._tooltipTimer);
this.setState({ isTooltipShown: false });
}
render() {
const { shouldPopupBeEnabled, isTooltipShown } = this.state;
const { message } = this.props;
return (
<ToolTip
message="Tooltip!!"
popoverOpen={shouldPopupBeEnabled && isTooltipShown}
>
<div
ref={this.titleParagraphRef}
onMouseOver={this._showTooltip}
>
{message}
</div>
</ToolTip>
);
}
}
This basically renders a floating tooltip over a div element if the message inside of it is bigger than the container. To do that, I use scrollWidth and clientWidth of the div element using a React reference. To detect those values I use componentDidMount, but this only works in full renders of the component. That is, if I have the component visible and reload the page, both values are equal to 0 and it does not work.
In addition, if I change the message, it does not work either because the component is already mounted.
So what I want is to change the state right after the component is mounted or updated so that the react reference is rendered and clientWidth and scrollWidth are not 0.
I have tried replace componentDidUpdate instead of componentDidMount but it's not a good practica to use setState inside componentDidUpdate.
Any solution?
First you should know that componentDidMount will execute only once. Therefor you can go for componentDidUpdate but don't forget to put a condition as it will render in a loop.
componentDidUpdate(prevProps,prevState) {
const shouldPopupBeEnabled = scrollWidth > clientWidth;
if (shouldPopupBeEnabled !== this.state.shouldPopupBeEnabled ) {
this.setState({shouldPopupBeEnabled });
}
}
Or you can go for functional components and use useEffect which will only render again if state changes.
useEffect(() => {
console.log('mounted');
}, [shouldPopupBeEnabled]) // It will re render id `shouldPopupBeEnabled` changes

React.js/ScrollMagic mount component after DOM modification

I'm trying to use ScrollMagic with React and I'm stuck with the setPin() function which wraps my component within a div. As the component in mounted before the wrapping, everytime I update my states or props, React seems to think that the component doesn't exist and mount it again.
How can I manage to mount my component after the setPin function which does the wrapping?
componentDidMount () {
this.scrollController = new ScrollMagic.Controller()
this.initScroller()
}
initScroller () {
this.scene = new ScrollMagic.Scene({
triggerElement: this.node,
duration: window.innerHeight * 2,
triggerHook: 0,
reverse: true
})
this.scene.setPin(this.node, { pushFollowers: false })
this.scrollController.addScene(this.scene)
}
render () {
return (
<div
ref={node => {
this.node = node
}}
>
<div className={this.getClassNames()}>
<Hemicycle
deputiesCount={this.props.deputiesCount}
swissCount={this.props.swissCount}
/>
</div>
</div>
)
}

Enzyme testing with React/Redux - shallow rendering issues with setState

I have a React component that loads another component if it has it's initial local state altered. I can't get a clean test going because I need to set local state AND shallow render so the child component doesn't crash when the new component mounts because the redux store isn't there. It seems those two objectives are incompatible in Enzyme.
For the child component to display, things need to occur:
The component needs to receive a "response" props (any string will do)
The component need to have it's initial "started" local state updated to true. This is done with a button in the actual component.
This is creating some headaches with testing. Here is the actual line that determines what will be rendered:
let correctAnswer = this.props.response ? <div className="global-center"><h4 >{this.props.response}</h4><Score /></div> : <p className="quiz-p"><strong>QUESTION:</strong> {this.props.currentQuestion}</p>;
Here is my current Enzyme test:
it('displays score if response and usingQuiz prop give proper input', () => {
const wrapper = shallow(<Quiz usingQuiz={true} answers={[]} response={'example'}/>);
wrapper.setState({ started: true })
expect(wrapper.contains(<Score />)).toEqual(true)
});
I am using shallow, because any time I use mount, I get this:
Invariant Violation: Could not find "store" in either the context or props of "Connect(Score)". Either wrap the root component in a <Provider>, or explicitly pass "store" as a prop to "Connect(Score)".
Because the component is displayed through the parent, I can not simply select the disconnected version. Using shallow seems to correct this issue, but then I can not update the local state. When I tried this:
it('displays score if response and usingQuiz prop give proper input', () => {
const wrapper = shallow(<Quiz usingQuiz={true} answers={[]} response={'example'}/>);
wrapper.setState({ started: true })
expect(wrapper.contains(<Score />)).toEqual(true)
});
The test fails because shallow doesn't let the DOM get updated.
I need BOTH conditions to be met. I can do each condition individually, but when I need to BOTH 1) render a component inside a component (needs shallow or will freak about the store not being there), and 2) update local state (needs mount, not shallow), I can't get everything to work at once.
I've looked at chats about this topic, and it seems this is a legitimate limitation of Enzyme, at least in 2017. Has this issue been fixed? It's very difficult to test this.
Here is the full component if anyone needs it for reference:
import React from 'react';
import { connect } from 'react-redux';
import { Redirect } from 'react-router-dom';
import { Transition } from 'react-transition-group';
import { answerQuiz, deleteSession, getNewQuestion } from '../../actions/quiz';
import Score from '../Score/Score';
import './Quiz.css';
export class Quiz extends React.Component {
constructor(props) {
super(props);
// local state for local component changes
this.state = {
started: false
}
}
handleStart() {
this.setState({started: true})
}
handleClose() {
this.props.dispatch(deleteSession(this.props.sessionId))
}
handleSubmit(event) {
event.preventDefault();
if (this.props.correctAnswer && this.props.continue) {
this.props.dispatch(getNewQuestion(this.props.title, this.props.sessionId));
}
else if (this.props.continue) {
const { answer } = this.form;
this.props.dispatch(answerQuiz(this.props.title, answer.value, this.props.sessionId));
}
else {
this.props.dispatch(deleteSession(this.props.sessionId))
}
}
render() {
// Transition styles
const duration = 300;
const defaultStyle = {
opacity: 0,
backgroundColor: 'rgba(0, 0, 0, 0.7)',
height: '100%',
width: '100%',
margin: '0px',
zIndex: 20,
top: '0px',
bottom: '0px',
left: '0px',
right: '0px',
position: 'fixed',
display: 'flex',
alignItems: 'center',
transition: `opacity ${duration}ms ease-in-out`
}
const transitionStyles = {
entering: { opacity: 0 },
entered: { opacity: 1 }
}
// Response text colors
const responseClasses = [];
if (this.props.response) {
if (this.props.response.includes("You're right!")) {
responseClasses.push('quiz-right-response')
}
else {
responseClasses.push('quiz-wrong-response');
}
}
// Answer radio buttons
let answers = this.props.answers.map((answer, idx) => (
<div key={idx} className="quiz-question">
<input type="radio" name="answer" value={answer} /> <span className="quiz-question-label">{answer}</span>
</div>
));
// Question or answer
let correctAnswer = this.props.response ? <div className="global-center"><h4 className={responseClasses.join(' ')}>{this.props.response}</h4><Score /></div>: <p className="quiz-p"><strong>QUESTION:</strong> {this.props.currentQuestion}</p>;
// Submit or next
let button = this.props.correctAnswer ? <button className="quiz-button-submit">Next</button> : <button className="quiz-button-submit">Submit</button>;
if(!this.props.continue) {
button = <button className="quiz-button-submit">End</button>
}
// content - is quiz started?
let content;
if(this.state.started) {
content = <div>
<h2 className="quiz-title">{this.props.title} Quiz</h2>
{ correctAnswer }
<form className="quiz-form" onSubmit={e => this.handleSubmit(e)} ref={form => this.form = form}>
{ answers }
{ button }
</form>
</div>
} else {
content = <div>
<h2 className="quiz-title">{this.props.title} Quiz</h2>
<p className="quiz-p">So you think you know about {this.props.title}? This quiz contains {this.props.quizLength} questions that will test your knowledge.<br /><br />
Good luck!</p>
<button className="quiz-button-start" onClick={() => this.handleStart()}>Start</button>
</div>
}
// Is quiz activated?
if (this.props.usingQuiz) {
return (
<Transition in={true} timeout={duration} appear={true}>
{(state) => (
<div style={{
...defaultStyle,
...transitionStyles[state]
}}>
{/* <div className="quiz-backdrop"> */}
<div className="quiz-main">
<div className="quiz-close" onClick={() => this.handleClose()}>
<i className="fas fa-times quiz-close-icon"></i>
</div>
{ content }
</div>
</div>
)}
</Transition >
)
}
else {
return <Redirect to="/" />;
}
}
}
const mapStateToProps = state => ({
usingQuiz: state.currentQuestion,
answers: state.answers,
currentQuestion: state.currentQuestion,
title: state.currentQuiz,
sessionId: state.sessionId,
correctAnswer: state.correctAnswer,
response: state.response,
continue: state.continue,
quizLength: state.quizLength,
score: state.score,
currentIndex: state.currentIndex
});
export default connect(mapStateToProps)(Quiz);
Here is my test using mount (this crashes due to a lack of store):
import React from 'react';
import { Quiz } from '../components/Quiz/Quiz';
import { Score } from '../components/Score/Score';
import { shallow, mount } from 'enzyme';
it('displays score if response and usingQuiz prop give proper input', () => {
const wrapper = mount(<Quiz usingQuiz={true} answers={[]} response={'example'}/>);
wrapper.setState({ started: true })
expect(wrapper.contains(<Score />)).toEqual(true)
});
});
This looks like a component that should be tested with mount(..).
How are you importing your connected component Score and Quiz?
I see that you are already correctly exporting Quiz component and default exporting the connected Quiz component.
Try importing with
import { Score } from '../Score/Score';
import { Quiz } from '../Quiz/Quiz';
in your test, and mount(..)ing. If you are importing from default export, you will get a connected component imported, which I think is the cause of the error.
Are you sure that Transition component let it's content to be displayed? I use this component and can't properly handle it in tests...
Can you for the test purposes alter your renders return with something like this:
if (this.props.usingQuiz) {
return (
<div>
{
this.state.started && this.props.response ?
(<Score />) :
(<p>No score</p>)
}
</div>
)
}
And your test can look something like this:
it('displays score if response and usingQuiz prop give proper input',() => {
const wrapper = shallow(<Quiz usingQuiz={true} answers={[]} response={'example'}/>);
expect(wrapper.find('p').text()).toBe('No score');
wrapper.setState({ started: true });
expect(wrapper.contains(<Score />)).toEqual(true);
});
I also tested shallows setState and little test like this works fine:
test('HeaderComponent properly opens login popup', () => {
const wrapper = shallow(<HeaderComponent />);
expect(wrapper.find('.search-btn').text()).toBe('');
wrapper.setState({ activeSearchModal: true });
expect(wrapper.find('.search-btn').text()).toBe('Cancel');
});
So I believe that shallow properly handle setState and the problem caused by some components inside your render.
The reason you are getting that error is because you're trying to test the wrapper component generated by calling connect()(). That wrapper component expects to have access to a Redux store. Normally that store is available as context.store, because at the top of your component hierarchy you'd have a <Provider store={myStore} />. However, you're rendering your connected component by itself, with no store, so it's throwing an error.
Also, if you are trying to test a component inside a component, may full DOM renderer may be the solution.
If you need to force the component to update, Enzyme has your back. It offers update() and if you call update() on a reference to a component that will force the component to re-render itself.

React router native animation blinks before animation starts

I'm developing a react native app and using React router native v4, and I'm trying to develop the animation part, as suggested by documentation, first I made sure that everything works without animations or transitions.
I've iterated the implementation and this is as far as I got by now:
my main component renders the following:
// app.js:render
<ConnectedRouter history={history}>
<App />
</ConnectedRouter>
my routes.js renders the following (note the location prop passed to Switch to prevent it updating its children before the parent component):
// routes.js render
<ViewTransition pathname={location.pathname}>
<Switch location={location}>
<Route exact path={uri.views.main} component={Dashboard} />
<Route path={uri.views.login} component={Login} />
<Route path={uri.views.register} component={Register} />
</Switch>
</ViewTransition>
and the ViewTransition that handles the animation, by now it just fadesin/out the old and the new views:
// view-transition.js
#withRouter
export default class ViewTransition extends Component {
static propTypes = {
children: PropTypes.node,
location: PropTypes.object.isRequired,
};
state = {
prevChildren: null,
opacity: new Animated.Value(1)
};
componentWillUpdate(nextProps) {
if (nextProps.location !== this.props.location) {
this.setState({ prevChildren: this.props.children }, this.animateFadeIn);
}
}
animateFadeIn = () => {
Animated.timing(this.state.opacity, {
toValue: 0,
duration: 150
}).start(this.animateFadeOut);
};
animateFadeOut = () => {
this.setState({ prevChildren: null }, () => {
Animated.timing(this.state.opacity, {
toValue: 1,
duration: 400
}).start();
});
};
render() {
const { children } = this.props;
const { prevChildren, opacity } = this.state;
return (
<Animated.View
style={{
...StyleSheet.absoluteFillObject,
opacity,
position: "absolute"
}}
>
{prevChildren || children}
</Animated.View>
);
}
}
The code above is working, I can see old view fading out and new view fading in, but I have an issue, when it starts fading out, somehow the component remounts again and I can see a blink just before the animation starts, I wish to know what's wrong with my code.
I could fix my code above, it happened that the method componentWillUpdate in the lifecycle of a react component, already had passed the nextProps to the children, and in the meantime my component sets the new state with old children, the Switch is preparing the render of the new ones, that produces an unmount of the oldChildren and the mount of the new children, when finally my component finishes to set the state, the old children already had been unmounted and they have to be mounted again.
The story above is the long story of "I can see a blink when my animation starts", the solution happened to be easy, I don't check stuff in componentWillUpdate anymore but in componentWillReceiveProps, since the new props will pass to the parent component before its children, it gives me enough time to catch the current children, assign them to the state, render before Switch unmounts them and keep them in the View for the fading out, so no blinking anymore.
My final view-transition.js:
// view-transition.js
export default class ViewTransition extends Component {
static propTypes = {
children: PropTypes.node,
location: PropTypes.object.isRequired,
};
state = {
prevChildren: null,
opacity: new Animated.Value(1)
};
componentWillReceiveProps(nextProps) {
if (nextProps.location !== this.props.location) {
this.setState({ prevChildren: this.props.children }, this.animateFadeIn);
}
}
animateFadeIn = () => {
Animated.timing(this.state.opacity, {
toValue: 0,
duration: 150
}).start(this.animateFadeOut);
};
animateFadeOut = () => {
this.setState({ prevChildren: null }, () => {
Animated.timing(this.state.opacity, {
toValue: 1,
duration: 400
}).start();
});
};
render() {
const { children } = this.props;
const { prevChildren, opacity } = this.state;
return (
<Animated.View
style={{
...StyleSheet.absoluteFillObject,
opacity,
position: "absolute"
}}
>
{prevChildren || children}
</Animated.View>
);
}
}
I wrote an AnimatedSwitch component to work in React Native.
There is a brief moment where the states are updating and it would flash, but I fixed that with this code:
// Need to render the previous route for the time between not animating and animating
if (needsAnimation && !animating) {
const tempPreviousRoute = getPreviousRoute(
exact,
previousLocation,
previousChildren,
);
if (tempPreviousRoute) {
const prevRouteComp = renderPreviousRoute(tempPreviousRoute);
return prevRouteComp;
} else {
return null;
}
}
Complete code is here.
https://javascriptrambling.blogspot.com/2020/07/react-router-native-animatedswitch.html
Hmm..i know it's little bit too late, maybe you can try react-router-native-animate-stack
It is subcomponent of react-router-native. But it is animatable and provide stack feature. It is inspired by switch component, if you know switch, you will know how to use this package.
Default behaviour
Customization

react-highcharts: canvas size changes when changing tabs

Description
I have a Highcharts line chart in a "progress" tab (the tab is a react-bootstrap Tab). When I open the "progress" tab, the chart appears at 100% width, which is good. When I toggle to a different tab, then come back to the "progress" tab, the chart's width changes from 100% to a fixed 600px. I went through the docs, both the official Highcharts and React-Highcharts, and could not find a solution.
I imagine there is a problem with the re-render of the component.
Code
highchart-line.js:
export default class HighChartLine extends Component {
constructor(props) {
super(props);
this.state = { config: undefined }; // or use null
}
getScoreData(eqId){
const self = this;
let url = 'src/data/dermatology/user_scores-eq' + eqId + ".json";
axios.get(url)
.then((response) => {
... //response informs
let config = {...};
self.setState({ config: config });
});
}
componentWillMount(){
this.getScoreData(this.props.equation.eqId);
}
componentWillReceiveProps(nextProps){
this.getScoreData(nextProps.equation.eqId);
}
render(){
// Handle case where the response is not here yet
if ( !this.state.config ) {
return <div>Loading...</div>
}
if ( this.state.config.length === 0 ) {
return <div>Sorry, no data was found for this equation.</div>;
} return (
<ReactHighcharts config={this.state.config} />
)
}
}
highchart-line gets imported into progress-tab.js:
const ProgressTab = ({equation, user}) => {
return(
<HighChartLine equation={equation} user={user} />
)
}; export default ProgressTab;
progress-tab gets imported into modal-main.js:
export default class ModalMain extends Component {
constructor(props) {
super(props);
this.state = {
tabKey: this.props.tabKey
};
}
handleSelect = (tabKey) => {
this.setState({ tabKey: tabKey });
};
render() {
return (
<Tabs activeKey={this.state.tabKey}
onSelect={this.handleSelect}
>
<Tab tabClassName="main-tab" eventKey={"progress"} >
<ProgressTab {...this.props} />
</Tab>
<Tab tabClassName="main-tab" eventKey={"peer-compare"}>
<PeerCompareTab {...this.props}/>
</Tab>
</Tabs>
)
}
}
Browser render
Width is correctly set to 100%
Width changes itself to 600px
Although this could hurt performance somewhat, I found that if I do not pass in the isPureConfig={true} prop into the ReactHighcharts component, it will re-render on tab select. Being that ReactHighcharts sets isPureConfig={false} by default and Highcharts itself reflows by default, it will all resize accordingly. The downside is you have to work with your charts re-rendering every time: https://github.com/kirjs/react-highcharts#limiting-highchart-rerenders
Of course, I am not certain it's any better to reflow the chart on every tab select anyway...

Resources