0

I am new to React. I have downloaded and installed code from this "CSS Modules and React" tutorial:-

Tutorial: https://css-tricks.com/css-modules-part-3-react/

Code: https://github.com/robinrendle/react-css-modules-boilerplate

The downloaded code is basically working as far as I can tell but I'm unable to get simple onClick events working inside Components.

I've tried all suggestions I can find on internet but none of the solutions have worked for me. Could you please help me get this working. Thank you.

package.json:-

{
  "name": "css-tricks-modules",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "dev": "webpack-dev-server --progress --colors",
    "start": "webpack && npm run dev"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "babel-core": "^6.7.4",
    "babel-loader": "^6.2.4",
    "babel-preset-es2015": "^6.6.0",
    "babel-preset-react": "^6.5.0",
    "browser-sync": "^2.12.8",
    "browser-sync-webpack-plugin": "^1.0.1",
    "css-loader": "^0.23.1",
    "extract-text-webpack-plugin": "^1.0.1",
    "react": "^15.0.2",
    "react-dom": "^15.0.2",
    "react-router": "^2.4.0",
    "static-site-generator-webpack-plugin": "^2.0.1",
    "webpack": "^1.12.14",
    "webpack-dev-server": "^1.14.1"
  },
  "dependencies": ""
}

webpack.config.js:-

var ExtractTextPlugin = require('extract-text-webpack-plugin'),
    StaticSiteGeneratorPlugin = require('static-site-generator-webpack-plugin'),
    BrowserSyncPlugin = require('browser-sync-webpack-plugin'),
    data = require('./data.js'),
    path = require('path');

module.exports = {
    entry: './src/router',
    output: {
        path: './build',
        filename: 'bundle.js',
        libraryTarget: 'umd',
        publicPath: '/'
    },
    module: {
        loaders: [
            {
                test: /\.js$/,
                loader: 'babel',
                // include: __dirname + '/src',
                include: path.join(__dirname,'src'),
            },
            {
                test: /\.css$/,
                loader: ExtractTextPlugin.extract('css?modules&importLoaders=1&localIdentName=[name]__[local]___[hash:base64:5]'),
                // include: __dirname + '/src'
                include: path.join(__dirname,'src')
            }
        ],
    },
    plugins: [
        new ExtractTextPlugin("styles.css"),
        new StaticSiteGeneratorPlugin('main', data.routes, data),
        new BrowserSyncPlugin({
            host: 'localhost',
            port: 3000,
            proxy: 'http://localhost:8080/'
        })
    ]
};

Home.js:-

import React from 'react'
import CoolButton from '../components/Button/Button'
import Counter from '../components/counter'
import App from '../components/app'
import App2 from '../components/app2'
import SayHello from '../components/sayHello'

export default class Home extends React.Component {
  render() {
    return (
        <div>
            <h1>Home page</h1>
            <p>This is a home page</p>
            <App />
            <App2 />
            <SayHello />
            <CoolButton text='A super cool button'/>
        </div>
    )
  }
}

sayHello.js (onClick event is not working):-

import React from 'react'

export default class SayHello extends React.Component {
  constructor(props) {
    super(props);
    this.handleClick = this.handleClick.bind(this);
  }

  handleClick() {
    console.log("Hello!!!");
    alert("Hello!!!!");
  }

  render() {
    return (
      <button onClick={this.handleClick}>
        Say hello
      </button>
    );
  }
}

app.js (onClick event not working):-

import React from 'react'

export default class App extends React.Component {

   constructor(props) {
      super(props);

      this.state = {
         data: 'Initial data...'
      }

      this.updateState = this.updateState.bind(this);

   };

   updateState() {
      this.setState({data: 'Data updated!!!'})
   }

   render() {
      return (
         <div>
            <button onClick = {this.updateState}>CLICK</button>
            <h4>{this.state.data}</h4>
         </div>
      );
   }
}

app2.js (onClick event not working):-

import React from 'react';

export default class App2 extends React.Component {

  constructor(props) {
    super(props);
    this.state = {isToggleOn: true};

    // This binding is necessary to make `this` work in the callback
    this.handleClick = this.handleClick.bind(this);
  }

  handleClick() {
    this.setState(prevState => ({
      isToggleOn: !prevState.isToggleOn
    }));
  }

  render() {
    return (
      <button onClick={this.handleClick}>
        {this.state.isToggleOn ? 'ON' : 'OFF'}
      </button>
    );
  }
}

The code compiles without errors and in Components "SayHello" and "App2" their initial state text is rendered but their onClick event isn't occurring or the data isn't updating. I think most likely the event isn't firing but I'm not sure.

I'd much appreciate advice on where/why the problem is occurring and how to fix. Thanks.

sqwunckly
  • 11
  • 1
  • 3
  • I created a new React project, added the Home.js, App.js, App2.js and SayHello.js files (and an index.js file where I render the whole thing). All the onClicks work just fine. Are you using the local npm server for testing? It usually shows you some useful information on what is going on. Additionally, I recommend getting the React dev tools chrome extension, you can pick apart your app and see all the component's states etc. https://chrome.google.com/webstore/detail/react-developer-tools/fmkadmapgofadopljbjfkapdkoienihi – EyfI Jun 18 '17 at 08:27
  • Hi Eyfl, much thanks for your help.. yes I'm using npm server.. is there a npm command for obtaining information on what this issue could be about? Here is all the compile information I get after building the project:- "Warning: Accessing PropTypes via the main React package is deprecated, and will be removed in React v16.0...Warning: Accessing createClass via the main React package is deprecated, and will be removed in React v16.0... – sqwunckly Jun 18 '17 at 08:38
  • I tried using React Dev Tools earlier tonight with this demo project on localhost:3000 and got msg saying "This page doesn't appear to be using React". Is there something else I need to do other than load the page (localhost:3000) and then click on the React Dev Tools icon on top right hand corner of page? – sqwunckly Jun 18 '17 at 08:52
  • Then there is definitely something wrong, the icon should be either blue (if the react on the page is in production mode) or red (if it is in developer mode). How did you create your application? Please try following the simple steps by the folks over at Facebook who work on React, you should be able to easily modify it to suit your needs: https://github.com/facebookincubator/create-react-app#create-react-app- – EyfI Jun 18 '17 at 09:09
  • thanks Eyfl I'll check out that facebook link you've provided.. the Css-Modules demo code I'm using has these lines in webpack.config.js "StaticSiteGeneratorPlugin = require('static-site-generator-webpack-plugin')" and "new StaticSiteGeneratorPlugin('main', data.routes, data)".. does the static site generated code mean it is no longer a react webpage? I am very new to this and am finding a lot of it very difficult to get going. Thanks again. – sqwunckly Jun 18 '17 at 09:43
  • I'm not familiar with 'static-site-generator-webpack-plugin', but from the looks of that tutorial you posted, its main focus doesn't seem to be React. I'd suggest looking through this wonderful React tutorial to get you started: https://reacttraining.com/online/react-fundamentals – EyfI Jun 18 '17 at 11:44
  • Hi Eyfl, thanks for your continuing help.. yes that tutorial was focused on setting up CSS-Modules within a React environment. I tried the 3 onClick events in a different React configuration & they work correctly there, so the problem evidently is with the configuration of the 'CSS-Modules with React' project. I really like CSS-Modules and want to get it (or another) CSS-Modules + React configuration working correctly but I am way out of my depth debugging which parts of the package.json or webpack.config configuration is causing the problem. Any suggestions on how a newbie can begin there? – sqwunckly Jun 18 '17 at 13:01
  • Also, thanks for the link to reacttraining.com/online/react-fundamentals .. I subscribed to it a week or so ago and I'm 2/3 through the course.. yes it is an excellent and well presented course. I'm learning a lot from it. – sqwunckly Jun 18 '17 at 13:07
  • Unfortunately, I have no experience using the CSS modules idea, so I cannot tell what is crucial in the above configs. I'd recommend trying to implement it into a small, already working, project. – EyfI Jun 19 '17 at 17:46

0 Answers0