0

I have a react class like this ..

class Questions extends React.Component {
   decodestr(decstr){
         var newstr = Base64.decode(decstr);
         return newstr;

   }
  render() {
     return(
      <div className="questions">
        {this.decodestr(this.props.cont)}
      </div>
      )
   }
}

I am trying to display decoded content inside a div like shown below :

<div> <span> <b>Questions Regulatory:</b></span> <span>{source.questions_regulatory}</span> </div>

where {source.questions_regulatory} is the encoded content.. How can I print the decoded content here.. Now am getting an error like this : ReferenceError: Base64 is not defined

I installed Base64 using npm install Base64 --save.

This is my webpack config ;

var path = require('path');
var webpack = require('webpack');
var HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
  devtool:"eval",
  context:path.join(__dirname),
  entry: [
    'webpack-hot-middleware/client?reload=true',
    './src/index.jsx'
  ],
  output: {
    path: path.join(__dirname, 'dist'),
    filename: 'bundle.js',
    publicPath: '/static'
  },
  plugins: [
    new webpack.HotModuleReplacementPlugin(),
    new webpack.NoErrorsPlugin()
  ],
  resolve: {
    alias: {
      react: path.resolve('./node_modules/react')
    },
    extensions:[".js", ".jsx", ".webpack.js", ".web.js",""]
  },
  resolveLoader: {
    root: path.join(__dirname, "node_modules")
  },
  module: {
    loaders: [
      {
        test: /\.jsx?$/,
        exclude: /(node_modules)/,
        loader: 'babel',
        query: {
          presets: [
            require.resolve('babel-preset-es2015'),
            require.resolve('babel-preset-react')
          ]
        }
      },
      {
        test: /\.scss$/,
        loaders: ["style", "css", "sass"]
      },
      {
        test: /\.(jpg|png|svg)$/,
        loaders: [
            'file-loader?name=[path][name].[ext]'
        ]
      }
    ]
  }
};

Not sure how to use this React class for rendering the decoded content..Also not sure why I get a refernce error...Any help appreciated...

nelz
  • 127
  • 1
  • 8
  • Have you `import`ed or `require`d the Base64 module anywhere in your React component? – Matt Holland Sep 21 '16 at 18:53
  • I tried using require , but it gave furthur errors.(Base64.decode is not a function)..I am having a hard time as am pretty new to this stuff..Very much appreciate it, if you can help me with this.. – nelz Sep 21 '16 at 18:57
  • import * as Base64 from "Base64"; I tried this import statement...gives me the same error Base64.decode is not a function – nelz Sep 21 '16 at 19:03
  • Base2.default.decode is not a function is what i get when I tried import Base64 from "Base64"; – nelz Sep 21 '16 at 19:05
  • If this is the module you installed: https://www.npmjs.com/package/base64 it may not work in the browser (It's a Node wrapper around C++ code, probably intended for server-side usage). You could try instead using the Base64 functionality that is built into modern browsers, as described in the answers to this question: http://stackoverflow.com/questions/246801/how-can-you-encode-a-string-to-base64-in-javascript – Matt Holland Sep 21 '16 at 23:33

0 Answers0