2017-10-21 11 views
0

두 가지 구성 요소가 있는데, 예를 들어 componentAcomponentB이라고합시다. 두 구성 요소 모두 .gif 파일을 가져 오는데, image.gif이라고 가정 해 보겠습니다. image.gif은 루프되지 않으므로 업데이트되지 않은 경우에만 재생해야합니다.GIF 파일을 다른 구성 요소가 반응에서 사용할 때 다시로드하지 못하도록합니다.

처음에는 componentAimage.gif이지만, componentB은 렌더링하지 않습니다. 따라서 image.gif 안에 componentA 한 번 재생됩니다. componentB 안에 image.gif을 렌더링하면 componentA 안에 image.gif이 다시 재생됩니다. 이는 원하지 않습니다.

할 수 있습니까? 감사!

편집 : 재현에 대한 몇 가지 간단한 코드 : './image.gif'

export default class componentA extends React.PureComponent { 
    render() { 
     return (
      <div> 
       <img src={image} /> 
      </div> 
     ); 
    } 
} 

Component B 이미지 가져 오기 './image.gif'로

Component A 이미지 가져 오기

export default class componentA extends React.PureComponent { 
    render() { 
     return (
      <div> 
       {this.props.show ? <img src={image} /> : null} 
      </div> 
     ); 
    } 
} 

App

import componentA from './componentA.react' 
import componentB from './componentB.react' 

export default class App extends React.Component { 

    componentWillMount() { 
     this.setState({ 
      show: false, 
     }); 
    } 

    componentDidMount() { 
     // ...or some moment when the App thinks componentB should update 
     this.setTimeout(() => { 
      this.setState({ 
       show: true, 
      }); 
     }, 4000); 
    } 

    render() { 
     return (
      <componentA /> 
      <componentB show={this.state.show} /> 
     ); 
    } 
} 
+0

하면, 몇 가지 코드를 제공시겠습니까 우린 당신을 도울 수 있어요? componentA와 B는 어떻게 구성됩니까? – 3Dos

+0

일부 샘플 추가 – Theogry

답변

0

좀 더 복잡하고 유연한 구조를 사용하는 것이 좋습니다. 정적 인 애니메이션 GIF를 사용하고 자바 스크립트로 바꾸십시오.

처럼 비슷한 경우에 제안했다 : this.setState으로

Stop a gif animation onload, on mouseover start the activation

JavaScript to control image animation?

+0

하지만 다른 GIF에 대해 다른 종료 시간이 있고 동적으로로드하는 경우 각 GIF의 종료 시간을 기록해야하므로 불가능할 수도 있습니다. – Theogry

0

this.render 방법을 실행하여이 개 구성 요소가 다시 렌더링된다.

당신이 시도 할 수있는 사항은 다음과 같습니다

구성 요소 A :

export default class componentA extends React.Component { 
    render() { 
     return (
      <div> 
       <img src={image} /> 
      </div> 
     ); 
    } 
} 

구성 요소 B :

export default class componentB extends React.Component { 
    constructor() { 
     super(props) 
     this.state = { show: false } 
    } 

    componentDidMount() { 
     this.setTimeout(() => { 
      this.setState({ 
       show: true, 
      }); 
     }, 4000); 
    } 

    render() { 
     return (
      <div> 
       {this.state.show ? <img src={image} /> : null} 
      </div> 
     ); 
    } 
} 

앱 :

import componentA from './componentA.react' 
import componentB from './componentB.react' 

export default class App extends React.Component { 
    render() { 
     // Carefull here, you have to return a single `node` in the render method 
     return (
      <div> 
       <componentA /> 
       <componentB /> 
      </div> 
     ); 
    } 
} 
+0

오해의 소지가있어서 몇 점 : 1. componentA와 componentB는 실제로 React.PureComponent를 확장하므로 소품이 변경되지 않으면 다시 렌더링되지 않습니다. 따라서 componentA는 결코 다시 렌더링하지 않아야합니다. 2. 구성 요소 B는 GIF를로드해야하는시기를 알 수 없으므로 시간 제한은 Grid에 의해 제어되어야합니다. 실수에 대해 사과드립니다. – Theogry

+0

오, 좋아, PureComponent 내 지식은 null과 같습니다! 나는 그것에게 모양을 줄 것이다! – 3Dos

+0

어쨌든 [docs] (https://reactjs.org/docs/react-api.html#reactpurecomponent)에서 다음을 읽으면 PureComponent로 렌더링됩니다. React 구성 요소의 render() 함수가 렌더링되면 동일한 소품과 주에서 동일한 결과가 나오면 어떤 경우 React.PureComponent를 사용하여 성능을 향상시킬 수 있습니다. 상태가 변경됨에 따라 실제로 모든 것이 다시 나타나게됩니다. 그래서 내가 문제를 해결하려고 ComponentB에서 주를 옮겼습니다! – 3Dos