2017-01-15 4 views
1

Ich helfe beim Erstellen einer Webapp und wir haben vor kurzem auf Stift umgeschaltet und reagieren. Ich habe alle Stylus-Lang-Dateien neu geschrieben und einige geschrieben, aber die Reaktion wird überhaupt nicht gestylt. Hier ist die Komponente unten.Stylus funktioniert nicht mit React

import React, { Component } from 'react' 
import Icon from 'react-component-bytesize-icons' 

class CoursesCard extends Component { 
    render() { 
     const divStyle = { 
      backgroundImage : '/assets/img/animatingCourse.jpg' 
     } 

     return (
      <div className='CoursesCard' style={divStyle}> 
      <div className='CardOverlay'> 
       <h3>What the courses is called?</h3> 
       <p>Last Checed</p> 
       <p>Users online</p> 
      </div> 
      </div> 
     ) 
    } 
} 

export default CoursesCard 

Hier ist der Griffel lang Code:

.CoursesCard { 
    width 32% 
    margin 10px 1% 
    float left 
    padding-top 20% 
    display inline-block 
    background white 
    box-shadow 0px 3px 20px #555 

    background-repeat no-repeat 
    background-size cover 
    cursor pointer 

    &.hover{ 
     box-shadow 0px 3px 22px #333 
    } 

.CardOverlay { 
    width 100% 
    height auto 
    padding 10px 0 
    background rgba(33,51,66,0.8) 
    color #fff 

    background-repeat no-repeat 
    background-size cover 
    cursor pointer 

    &.hover { 
    box-shadow 0px 3px 22px #333 
    } 

    h3{ 
    font-size 18px 
    font-weight 500 
     margin-bottom 5px 
    margin-left 10px 
    } 
    p.lastChecked{ 
    font-size 13px 
     font-weight 100 
     margin-bottom 5px 
     margin-left 10px 
    } 
    p.onlineUsers{ 
    font-size 14px 
     font-weight 400 
     margin-left 10px 
     display block 
    } 
} 

}

Antwort

1

gar nichts zu tun hat Reagieren mit Stylus.

Ich nehme an, dass Sie Webpack als Bundler der Wahl verwenden. Wenn dies der Fall ist, müssen Sie Webpack so konfigurieren, dass es weiß, was mit .styl Dateien geschehen soll (oder welche Dateierweiterung verwendet wird).

Webpack hat Konzept von Loader - Loader lädt Datei, wandelt es in irgendeiner Weise und schiebt die Ausgabe in der Kette.

Sie müssen Stylus-Lader hinzufügen. Um dies zu tun, sehen Sie sich bitte dieses Plugin GH page

1

Using Stylus in React erfordert eine Bundler wie Webpack. Ich benutze Stylus + React viel, damit ich dir helfen kann aber deine Frage sagt mir nicht, ob du Webpack und NPM oder Garn eingerichtet hast.

Da ich Ihren vollen Build nicht sehen kann, kann ich meinen Build teilen und vielleicht wird das helfen. Dies ist der nächste Schritt, um Ihnen Schritt für Schritt zu helfen, ohne mehr über Ihren aktuellen Build zu erfahren. Hoffe das hilft!

Angenommen, Sie haben Webpack und npm einrichten und Ihr Projekt hat bereits eine package.json Datei ...

  1. Führen Sie diese Befehle in Terminal ...

npm install yarn

yarn init

yarn add poststylus autoprefixer-stylus autoprefixer 

css-loader poststylus Sass-loader Stil-loader Griffel-loader

  1. Machen Sie Ihre webpack.config.js ähnlich wie diese ...

    const path = require('path'); 
    const postStylus = require('poststylus'); 
    const webpack = require('webpack'); 
    
    module.exports = { 
        context: __dirname, 
        entry: [ 
        './source/ClientApp.jsx', 
        'react-hot-loader/patch', 
        'webpack-dev-server/client?http://localhost:8080/', 
        'webpack/hot/only-dev-server', 
        ], 
        devtool: 'cheap-eval-source-map', 
        output: { 
        path: path.join(__dirname, 'public'), 
        filename: 'bundle.js', 
        publicPath: '/public/', 
        hotUpdateChunkFilename: 'hot/hot-update.js', 
        hotUpdateMainFilename: 'hot/hot-update.json' 
        }, 
        devServer: { 
        hot: true, 
        publicPath: '/public/', 
        historyApiFallback: true, 
        }, 
        watch: true, 
        plugins: [ 
        new webpack.HotModuleReplacementPlugin(), 
        new webpack.NamedModulesPlugin(), 
        // new webpack.optimize.ModuleConcatenationPlugin(), 
        new webpack.LoaderOptionsPlugin({ 
         options: { 
         stylus: { 
          preferPathResolver: 'webpack', 
         }, 
         } 
        }), 
        ], 
        resolve: { 
        extensions: ['.js', '.jsx', '.json', '.styl'], 
        modules: [ 
         path.resolve('./source'), 
         path.resolve('./node_modules'), 
        ], 
        }, 
        stats: { 
        colors: true, 
        reasons: true, 
        chunks: true, 
        }, 
        module: { 
        rules: [ 
         { 
         enforce: 'pre', 
         test: /\.js$/, 
         loader: 'eslint-loader', 
         exclude: /node_modules/, 
         }, 
         { 
         test: /\.jsx?$/, 
         loader: 'babel-loader', 
         }, 
         { 
         test: /\.styl$/, 
         use: [ 
          'style-loader', 
          'css-loader', 
          { 
          loader: 'stylus-loader', 
          options: { 
           use: [ 
           postStylus(['autoprefixer']), 
           ], 
           modules: true, 
          }, 
          }, 
         ], 
         exclude: /node_modules/, 
         }, 
        ], 
        }, 
    }; 
    
  2. Und hier ist meine ganze package.json, wenn Sie es als Referenz benötigen ...

    { 
        "scripts": { 
        "lint": "eslint **/*.{js,jsx} --quiet", 
        "clean": "rm -r public/bundle.js", 
        "build": "rm -r public/bundle.js && webpack", 
        "dev": "webpack-dev-server --watch --progress --colors", 
        "watch": "webpack --watch" 
        }, 
        "dependencies": { 
        "autoprefixer": "^7.1.2", 
        "autoprefixer-stylus": "^0.14.0", 
        "axios": "^0.16.2", 
        "babel-core": "^6.25.0", 
        "babel-eslint": "^7.2.3", 
        "babel-loader": "^7.1.1", 
        "babel-plugin-dynamic-import-node": "^1.0.2", 
        "babel-plugin-dynamic-import-webpack": "^1.0.1", 
        "babel-plugin-syntax-dynamic-import": "^6.18.0", 
        "babel-plugin-transform-class-properties": "^6.24.1", 
        "babel-plugin-transform-decorators-legacy": "^1.3.4", 
        "babel-plugin-transform-es2015-modules-commonjs": "^6.24.1", 
        "babel-preset-airbnb": "^2.4.0", 
        "babel-preset-env": "^1.6.0", 
        "babel-preset-react": "^6.24.1", 
        "babel-register": "^6.24.1", 
        "concurrently": "^3.5.0", 
        "css-loader": "^0.28.4", 
        "d3": "^4.10.0", 
        "enzyme": "^2.9.1", 
        "eslint": "3.19.0", 
        "eslint-config-airbnb": "^15.0.2", 
        "eslint-config-airbnb-base": "^11.2.0", 
        "eslint-config-react": "^1.1.7", 
        "eslint-loader": "^1.9.0", 
        "eslint-plugin-import": "^2.7.0", 
        "eslint-plugin-jsx-a11y": "^5.0.3", 
        "eslint-plugin-react": "^7.1.0", 
        "express": "^4.15.3", 
        "extract-text-webpack-plugin": "^3.0.0", 
        "firebase": "^4.2.0", 
        "firebase-tools": "^3.9.2", 
        "json-loader": "^0.5.7", 
        "node-sass": "^4.5.3", 
        "nodemon": "^1.11.0", 
        "poststylus": "^0.2.3", 
        "prop-types": "^15.5.10", 
        "react": "^15.6.1", 
        "react-date-range": "^0.9.4", 
        "react-dom": "^15.6.1", 
        "react-hot-loader": "3.0.0-beta.6", 
        "react-router-dom": "^4.1.2", 
        "react-test-renderer": "^15.6.1", 
        "resolve-url-loader": "^2.1.0", 
        "sass-loader": "^6.0.6", 
        "sort-package-json": "^1.7.0", 
        "style-loader": "^0.18.2", 
        "stylus": "^0.54.5", 
        "stylus-loader": "^3.0.1", 
        "webpack": "2.6.1", 
        "webpack-combine-loaders": "^2.0.3", 
        "webpack-dev-middleware": "^1.11.0", 
        "webpack-dev-server": "^2.6.1", 
        "webpack-hot-middleware": "^2.18.2" 
        }, 
    }