Reactstrap Card component throws error - reactjs

The issue is with the reactstrap cards: https://reactstrap.github.io/components/card/
Using reactstrap components like Card, etc is not working.
Component: Version I
import React, { Component } from 'react';
import { Card, Button, CardImg, CardTitle, CardText, CardDeck, CardSubtitle, CardBody } from 'reactstrap';
class MoviesIndex extends Component {
render() {
return (
<CardDeck>
<Card>
<CardImg top width="100%" src="" alt="Movie Poster" />
</Card>
</CardDeck>
);
}
}
export default MoviesIndex;
Output: *It works fine without any errors.
But when I try to use the rest of the components from reactstrap. It throws errors on console.
Component: Version II
import React, { Component } from 'react';
import { Card, Button, CardImg, CardTitle, CardText, CardDeck, CardSubtitle, CardBody } from 'reactstrap';
class MoviesIndex extends Component {
render() {
return (
<CardDeck>
<Card>
<CardImg top width="100%" src="" alt="Movie Poster" />
<CardBody>
<CardTitle>Card title</CardTitle>
<CardSubtitle>Card subtitle</CardSubtitle>
<CardText>This is a wider card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.</CardText>
<Button>Button</Button>
</CardBody>
</Card>
</CardDeck>
);
}
}
export default MoviesIndex;
Output:
package.json
{
"name": "client",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"start": "node ./node_modules/webpack-dev-server/bin/webpack-dev-server.js",
"test": "mocha --compilers js:babel-core/register --require ./test/test_helper.js --recursive ./test",
"test:watch": "npm run test -- --watch"
},
"devDependencies": {
"babel-core": "^6.2.1",
"babel-loader": "^6.2.0",
"babel-preset-es2015": "^6.1.18",
"babel-preset-react": "^6.1.18",
"chai": "^3.5.0",
"chai-jquery": "^2.0.0",
"jquery": "^2.2.1",
"jsdom": "^8.1.0",
"mocha": "^2.4.5",
"react-addons-test-utils": "^0.14.7",
"webpack": "^1.12.9",
"webpack-dev-server": "^1.14.0"
},
"dependencies": {
"axios": "^0.17.1",
"babel-preset-stage-1": "^6.1.18",
"lodash": "^3.10.1",
"react": "^16.2.0",
"react-dom": "^16.2.0",
"react-redux": "^5.0.6",
"react-router": "^4.2.0",
"react-transition-group": "^2.2.1",
"reactstrap": "^4.8.0",
"redux": "^3.7.2",
"redux-promise": "^0.5.3"
}
}
I am not able to debug this issue. Please help! TIA.

The components such as CardBody, etc are not available in v4.8.0.
Upgrading to latest release (reactstrap v5.0.0-alpha.4) resolves this issue!
npm install --save reactstrap#5.0.0-alpha.4
Refer to the issue that I created on reactstrap#github for more details:
https://github.com/reactstrap/reactstrap/issues/730

This looks like a typo here:
renderMovies() {
return _.map(this.props.movies, movie => {
Missing )
Try
_.map(this.props.movies, movie) => {
If that doesn't do it, put console.log() after everything to see where it becomes undefined.
This type of error usually results from something not being exported correctly or something being null/undefined due to data missing, etc.
This is a good opportunity for me to recommend using ES Lint, so you can gain extra help 24/7 from the passive display of errors. Check it out if you aren't using it. It's absolutely worth looking into and using.
I won't recommend any specific linting configs in here. That is out of scope of this help. ES Lint will underline code errors such as the missing ) with a red underline, so you will experience less of this kind of thing :)

Related

webpack 5: magic comments not working for images

I am trying to preload/prefetch an image so that I don't have to be connected to the internet at the exact moment to display the image. It's for a "you are offline" page in my app.
My app is written in React and I am using webpack for my bundler.
I was reading online that the webpack magic comments feature might be able to do that.
https://medium.com/webpack/link-rel-prefetch-preload-in-webpack-51a52358f84c
It's not working for me.
The image that I am trying to preload/prefetch does not show and there is no <link rel tag added to the DOM.
Checkout the demo repository here:
https://github.com/aubreyquinn/preload-webpack-demo/tree/main
package.json:
"devDependencies": {
"#babel/core": "^7.19.6",
"#babel/preset-env": "^7.19.4",
"#babel/preset-react": "^7.18.6",
"babel-loader": "^8.2.5",
"css-loader": "^6.7.1",
"file-loader": "^6.2.0",
"html-webpack-plugin": "^5.5.0",
"style-loader": "^3.3.1",
"webpack": "^5.74.0",
"webpack-cli": "^4.10.0",
"webpack-dev-server": "^4.11.1"
},
"dependencies": {
"react": "^18.2.0",
"react-dom": "^18.2.0"
}
You have to use the useState hook to trigger the re-render, The webpack prefetching is working fine but react is not re-rendering since it is unaware of such loading.
import React, { useEffect, useState } from "react";
import Flowers from "../assets/flowers.jpg";
// Home component
const Home = () => {
const [lilies, setLilies] = useState(null);
useEffect(() => {
import(/* webpackPrefetch: true */ "../assets/lilies.jpg").then((res) => {
console.log(res);
setLilies(res.default);
});
}, []);
return (
<>
<div>React Application</div>
<img src={Flowers} height={200} />
<img src={lilies} height={200} />
</>
);
};
export default Home;
Try the above code out, it's working just fine.

How to fix Typescript - Next.js build error?

When i build my small Typescript - Nextjs project,
occur type error like this
./components/Layout.tsx:3:55
Type error: Cannot find namespace 'React'.
1 | import NavBar from "#components/NavBar";
2 |
> 3 | export default function Layout({children} :{children :React.ReactNode}){
| ^
4 | return (
5 | <>
6 | <NavBar/>
info - Checking validity of types .%
Layout.tsx is only
import NavBar from "#components/NavBar";
export default function Layout({children} :{children :React.ReactNode}){
return (
<>
<NavBar/>
<div>
{children}
</div>
</>
)
}
and _app.tsx is
import '#/styles/globals.css'
import type { AppProps } from 'next/app'
import Layout from '#components/Layout'
import { Provider } from 'react-redux'
import store from '#/store'
function MyApp({ Component, pageProps }: AppProps) {
return (
<Provider store={store}>
<Layout>
<Component {...pageProps} />
</Layout>
</Provider>
)
}
export default MyApp
and package.json is
{
"name": "bi-front",
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "NODE_ENV=development next dev",
"build": "NODE_ENV=development next build",
"start": "NODE_ENV=production next start",
"lint": "NODE_ENV=development next lint"
},
"dependencies": {
"#reduxjs/toolkit": "^1.8.3",
"#types/react-datepicker": "^4.4.2",
"#types/react-redux": "^7.1.24",
"apexcharts": "^3.35.3",
"autoprefixer": "^10.4.7",
"axios": "^0.27.2",
"dayjs": "^1.11.3",
"dotenv": "^16.0.1",
"mongoose": "^6.4.0",
"next": "12.1.6",
"path": "^0.12.7",
"postcss": "^8.4.14",
"react": "^18.2.0",
"react-apexcharts": "^1.4.0",
"react-csv-downloader": "^2.8.0",
"react-datepicker": "^4.8.0",
"react-dom": "^18.2.0",
"react-redux": "^8.0.2",
"react-select": "^5.3.2",
"redux": "^4.2.0",
"tailwindcss": "^3.1.3",
"typesafe-actions": "^5.1.0"
},
"devDependencies": {
"#types/node": "18.0.0",
"#types/react": "^18.0.15",
"#types/react-dom": "18.0.5",
"eslint": "8.18.0",
"eslint-config-next": "12.1.6",
"typescript": "4.7.4"
}
}
The reason why i use not only _app.tsx but Layout.tsx is more simply Managing Components.
I think this is simple error,
but this is my first Typescript - Nextjs project,
so i don't know how to fix it.
Please understand that i'm not good at english,
PLEASE HELP ME :(
You have to import React on top of your file, or Import the exact type and use it, e.g.
import { ReactNode } from 'react';
export default function Layout({children}: {children: ReactNode}) {
/* OR */
import React from 'react';
export default function Layout({children}: {children: React.ReactNode}) {

How can Tailwind/typography work well with markdown-it in a React project?

I'm attempted to develop a new feature for my blog, that is a Markdown editor for writing articles.
I chosed #tailwindcss/typography and markdown-it to do that, so this is my whole dependencies:
package.json
{
"dependencies": {
"firebase": "^9.0.0-beta.7",
"markdown-it": "^12.2.0",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-router-dom": "^5.2.0"
},
"devDependencies": {
"#babel/core": "^7.15.0",
"#babel/preset-env": "^7.15.0",
"#babel/preset-react": "^7.14.5",
"#tailwindcss/typography": "^0.4.1",
"autoprefixer": "^10.3.2",
"babel-loader": "^8.2.2",
"css-loader": "^6.2.0",
"dotenv-webpack": "^7.0.3",
"html-webpack-plugin": "^5.3.2",
"postcss": "^8.3.6",
"postcss-cli": "^8.3.1",
"postcss-loader": "^6.1.1",
"style-loader": "^3.2.1",
"tailwindcss": "^2.2.7",
"webpack": "^5.51.1",
"webpack-cli": "^4.8.0",
"webpack-dev-server": "^4.0.0"
}
}
Below code is the component for this feature, including a editing area and a preview area. However, it didn't work.
When I run this code, it is rendered out like this, without typographing the <h1> tag.
However, if I repalce md.render(markdown) with <h1>hello</h1>(the markdown-it's rendering result), it seems "work", looking like this.
Editor.jsx
import React, { useState } from "react";
const md = require("markdown-it")('commonmark');
const Editor = () => {
const [markdown, setMarkdown] = useState("# hello");
const onTextChange = (e) => {
setMarkdown(e.target.value);
}
return (
<div>
<form>
<textarea onChange={(e) => onTextChange(e)}>
{markdown}
</textarea>
</form>
<div id="preview" className="prose">
{md.render(markdown)} {/* <h1>hello</h1> */}
</div>
</div>
)
}
export default Editor;
Why these things happened? and how can I make it run with expections?
use react-markdown instead of markdown-it
here is an example:
import ReactMarkdown from "react-markdown";
<div className="prose">
<ReactMarkdown>{markdown}</ReactMarkdown>
</div>
it will render the provided content as DOM in the page, and Tailwind/typography perfectly styles those elements.

Material-UI AppBar causes invalid tag error

I'm trying to insert AppBar into my component. While all other material-ui components work fine, this one causes the following error:
Uncaught Error: Invalid tag: /f7f7800bcef3562e6f4df1583b20797b.js
at invariant (bundle.js:724)
at validateDangerousTag (bundle.js:47303)
at new ReactDOMComponent (bundle.js:47330)
at Object.createInternalComponent (bundle.js:20522)
at instantiateReactComponent (bundle.js:21633)
at instantiateChild (bundle.js:46812)
at bundle.js:46839
at traverseAllChildrenImpl (bundle.js:21875)
at traverseAllChildrenImpl (bundle.js:21891)
at traverseAllChildrenImpl (bundle.js:21891)
That is a piece of code with AppBar:
import React, { Component } from 'react';
import { List, ListItem } from 'material-ui/List';
import AppBar from 'material-ui/AppBar';
class Contacts extends Component {
render() {
return (
<div>
<AppBar title="title"/>
<List>
<ListItem primaryText="text" secondaryText="text"/>
</List>
</div>
);
}
}
export default Contacts;
When i remove <AppBar ... /> everything works fine.
These are my dependecies:
"dependencies": {
"axios": "^0.15.3",
"events": "^1.1.1",
"font-awesome": "^4.7.0",
"material-design-icons": "^3.0.1",
"material-ui": "^0.17.0",
"react": "^15.4.0",
"react-dom": "^15.4.0",
"react-router": "^3.0.2",
"react-tap-event-plugin": "^2.0.1",
"roboto-fontface": "^0.7.0",
"rxjs": "^5.1.1"
},
"devDependencies": {
"babel-core": "^6.23.1",
"babel-loader": "^6.3.2",
"babel-preset-es2015": "^6.22.0",
"babel-preset-react": "^6.23.0",
"css-loader": "^0.26.1",
"extract-text-webpack-plugin": "^2.0.0-rc.3",
"file-loader": "^0.10.0",
"html-webpack-plugin": "^2.28.0",
"webpack": "^2.2.1",
"webpack-dev-server": "^2.3.0"
}
The same error occurs when i try to use compoents from pre-built SVG Icon set.

Uncaught TypeError: (0 , _reactRedux.bindActionCreators) is not a functin

I'm playing around with redux, I'm and started getting this error, can't resolve it, I was hoping it has any meaning, but can't seem to find any information.. Can anyone explain the reason this may show? Thanks..
Code:
import React, { Component } from 'react';
import { connect } from 'react-redux';
import { bindActionCreators } from 'react-redux';
import { searchForBeers } from '../../actions/index';
class SearchBar extends Component {
constructor(props) {
super(props);
this.state = { term: ''}
this.onFieldChange = this.onFieldChange.bind(this);
this.onClickSearch = this.onClickSearch.bind(this);
}
onFieldChange(event) {
this.setState({ term: event.target.value })
console.log(this.state)
}
onClickSearch() {
console.log(this.state)
this.props.searchForBeers(this.state.term)
}
render() {
return (
<div className="col-lg-6" style={{top:20 , width:'70%'}}>
<div className="input-group">
<span className="input-group-btn">
<button onClick={this.onClickSearch} className="btn btn-secondary" type="button">Go!</button>
</span>
<input type="text" className="form-control" placeholder="Search for..." onChange={this.onFieldChange} />
</div>
</div>
)
}
}
//Container functions
function mapDispatchToProps(dispatch) {
return bindActionCreators({ searchForBeers }, dispatch);
}
export default connect(null, mapDispatchToProps)(SearchBar);
Package json:
{
"name": "redux-simple-starter",
"version": "1.0.0",
"description": "Simple starter package for Redux with React and Babel support",
"main": "index.js",
"repository": "git#github.com:StephenGrider/ReduxSimpleStarter.git",
"scripts": {
"start": "node ./node_modules/webpack-dev-server/bin/webpack-dev-server.js"
},
"author": "",
"license": "ISC",
"devDependencies": {
"babel-core": "^6.2.1",
"babel-loader": "^6.2.0",
"babel-preset-es2015": "^6.1.18",
"babel-preset-react": "^6.1.18",
"chai": "^3.5.0",
"chai-jquery": "^2.0.0",
"css-loader": "^0.25.0",
"jquery": "^2.2.1",
"jsdom": "^8.1.0",
"mocha": "^2.4.5",
"node-sass": "^3.10.0",
"react-addons-test-utils": "^0.14.7",
"sass-loader": "^4.0.2",
"style-loader": "^0.13.1",
"webpack": "^1.13.2",
"webpack-dev-server": "^1.14.0"
},
"dependencies": {
"axios": "^0.13.1",
"babel-preset-stage-1": "^6.1.18",
"lodash": "^3.10.1",
"material-ui": "^0.15.4",
"raw-loader": "^0.5.1",
"react": "^15.3.2",
"react-dom": "^15.3.2",
"react-redux": "^4.4.5",
"react-router": "^2.0.1",
"redux": "^3.5.2",
"redux-promise": "^0.5.0",
"redux-simple-promise": "^2.0.2"
}
}
It's the redux module that provides the bindActionCreators function so the correct way to import it would be
import { bindActionCreators } from 'redux';
instead of importing it from react-redux.
There is a page with examples in the docs over at their API documentation site.
The error you're seeing simply looks funky because of the way babel transpiles ES2015 modules but it's not actually React or Redux that's throwing it.

Resources