2016-11-28 7 views
0

私は気象APIから小道具を渡されるダムコンポーネントを持っています。私は、APIから返されるものに応じてSVGイメージを動的に変更したいと考えています。 npmモジュールをインストールしましたreact-svghttps://github.com/atomic-app/react-svgこれは私がインストールしたsvg-injectorhttps://www.npmjs.com/package/svg-injectorの依存関係を持っています。今、私はreact-svg - >import ReactSVG from 'react-svg';を輸入しています。私はその後、私のダムコンポーネント内でそれを実装:動的にsvgをインポートする

const WeatherReport = ({report}) => { 
return (
    <div style={styles.body} className="row"> 
     <div style={styles.weatherBoxContainer}> 
      <div className="col-sm-2 col-md-offset-1" style={styles.weatherBoxContainer.weatherCard}> 
       <div style={styles.weatherBoxContainer.weatherReport}> 
        <ReactSVG path={'RELATIVE TO DOCUMENT ROOT I'M SERVING FROM'} callback={(svg) => console.log(svg)} /> 
        <div className="row" style={styles.weatherBoxContainer.temps}> 
         <div className="col-sm-4" style={styles.weatherBoxContainer.tempMinMax}> 
          <div>{Math.floor(report.list[0].main.temp_max)}°</div> 
          <div>{Math.floor(report.list[0].main.temp_min)}°</div> 
         </div> 
         <div className="col-sm-8" style={styles.weatherBoxContainer.currentTemp}> 
          {Math.floor(report.list[0].main.temp)}° 
         </div> 
        </div> 
       </div> 
       CA 
      </div> 
      <div className="col-sm-2" style={styles.weatherBoxContainer.weatherCard}> 
       <div style={styles.weatherBoxContainer.weatherReport}> 
        <div className="row" style={styles.weatherBoxContainer.temps}> 
         <div className="col-sm-4" style={styles.weatherBoxContainer.tempMinMax}> 
          <div>{Math.floor(report.list[1].main.temp_max)}°</div> 
          <div>{Math.floor(report.list[1].main.temp_min)}°</div> 
         </div> 
         <div className="col-sm-8" style={styles.weatherBoxContainer.currentTemp}> 
          {Math.floor(report.list[1].main.temp)}° 
         </div> 
        </div> 
       </div> 
       UT 
      </div> 
      <div className="col-sm-2" style={styles.weatherBoxContainer.weatherCard}> 
       <div style={styles.weatherBoxContainer.weatherReport}> 
        <div className="row" style={styles.weatherBoxContainer.temps}> 
         <div className="col-sm-4" style={styles.weatherBoxContainer.tempMinMax}> 
          <div>{Math.floor(report.list[2].main.temp_max)}°</div> 
          <div>{Math.floor(report.list[2].main.temp_min)}°</div> 
         </div> 
         <div className="col-sm-8" style={styles.weatherBoxContainer.currentTemp}> 
          {Math.floor(report.list[2].main.temp)}° 
         </div> 
        </div> 
       </div> 
       MN 
      </div> 
      <div className="col-sm-2" style={styles.weatherBoxContainer.weatherCard}> 
       <div style={styles.weatherBoxContainer.weatherReport}> 
        <div className="row" style={styles.weatherBoxContainer.temps}> 
         <div className="col-sm-4" style={styles.weatherBoxContainer.tempMinMax}> 
          <div>{Math.floor(report.list[3].main.temp_max)}°</div> 
          <div>{Math.floor(report.list[3].main.temp_min)}°</div> 
         </div> 
         <div className="col-sm-8" style={styles.weatherBoxContainer.currentTemp}> 
          {Math.floor(report.list[3].main.temp)}° 
         </div> 
        </div> 
       </div> 
       DC 
      </div> 
      <div className="col-sm-2" style={styles.weatherBoxContainer.weatherCard}> 
       <div style={styles.weatherBoxContainer.weatherReport}> 
        <div className="row" style={styles.weatherBoxContainer.temps}> 
         <div className="col-sm-4" style={styles.weatherBoxContainer.tempMinMax}> 
          <div>{Math.floor(report.list[4].main.temp_max)}°</div> 
          <div>{Math.floor(report.list[4].main.temp_min)}°</div> 
         </div> 
         <div className="col-sm-8" style={styles.weatherBoxContainer.currentTemp}> 
          {Math.floor(report.list[4].main.temp)}° 
         </div> 
        </div> 
       </div> 
       NY 
      </div> 
     </div> 
    </div> 
); 
}; 

WeatherReport.propTypes = { 
report: PropTypes.object 
}; 

export default WeatherReport; 

を今、ReactSVGのパスはあなたがReactSVGが含まれているのjsファイルへの相対はありませんから提供しているドキュメントルートからの相対である必要があります。十分に簡単ですか?しかし、私はバベルを使用しており、すべてがJSとして1つのファイルとして提供されています。私は全く新しいwebpackbabelreactreduxということについては全く新しいです...私がsvgへのパスを1つのファイルにコンパイルしているときに私がどのようにパスすると思いますか?

答えて

0

webpackのビルドステップからの出力がルート外の/dist/フォルダにあると仮定すると、svgファイルなど、そのフォルダ内の他の外部ファイルをコピーするビルドステップが必要な場合もあります。

/dist 
|- bundle.js 
|- myart.svg 

次に、あなたのファイルには、単に

<ReactSVG path="myart.svg" /> 
として svgを参照することができます
関連する問題