how can I extend a react-bootstrap component - reactjs

I want to extend the react-bootstrap component and modify some of the prototype to achive my component.Here is my code:
import React from 'react';
import { Pagination } from 'react-bootstrap';
export default class myPagination extends Pagination {
constructor(props) {
super(props);
}
render() {
return <div>test</div>;
}
}
But here is a error:Cannot read property 'bind' of undefined
Maybe the Pagination component is created by React.createClass so I can't extends the component?
How can I solve the problem?

Related

My class component is not displaying anything

So, I just started with react and i'm having troubles displaying my class component in the browser.
Here's my code...
import React, { component } from 'react'
class Hello extends component {
render() {
return <h1>class component</h1>
}
}
export default Hello
The problem is in component, it should be Component (in PascalCase)

How to use eval() to import components dynamically in react-native

I am trying to import a class component from a js file placed in external storage dynamically,
sample.js
import React, { Component } from 'react';
import { View, Text } from 'react-native';
var HeaderComponent=eval(`class HeaderComponent extends Component {
constructor(props) {
super(props);
}
render() {
return <Text>This is header</Text>;
}
}`)
class App extends Component {
constructor(props) {
super(props);
}
render() {
return <View>{HeaderComponent}</View>;
}
}
when i run the above code, I got following error,
SyntaxError: Unexpected token '<'
this error apparently indicates the following return statement(which is inside eval()),
return <Text>This is header</Text>;
May i know if this is the right way of doing dynamic import?. If not, Please let me know the right way of doing the dynamic import in react-native.
The eval() is basically a function that is used for doing Arithmetic operations, It can’t used for import a Component or any thing else,
here's a link from where you can learn about eval()
For import component you can do that with several ways
import statement:
import <Your Component Name> from '<Your Component Path>'
require()
const <Your Component Name> = require('<Your Component Path>')
you just need to add one of the above line at top of the files where you've declared other imports
use import/export keywords. add export at the end of the file, and import at the top
import React, { Component } from "react";
import { Text, View } from "react-native";
class HeaderComponent extends Component {
constructor(props) {
super(props);
}
render() {
return <Text>This is header</Text>;
}
}
class Test extends Component {
constructor(props) {
super(props);
}
render() {
return <View><HeaderComponent/></View>;
}
}
export default Test;

Referencing a React-Redux TypeScript component in another TypeScript component causes a "no export member" error

I have the following TypeScript (v3.01) React component named TopBar to which I'm now adding React-Redux. The TopBar component is then referenced in a parent component named Layout.
import * as React from 'react';
import { connect } from "react-redux";
import { decrementZoomLevel, incrementZoomLevel, setCenterPoint, setZoomLevel } from '../store/actions';
class TopBar extends React.Component<any, any>{
...
};
/*
* Redux-React setup
*/
const mapStateToProps = (state: any): any => {
return {
centerPoint: state.centerPoint,
zoomLevel: state.zoomLevel
}
}
export default connect(mapStateToProps)(TopBar);
However, when I reference this component in another Layout TypeScript component
import * as React from 'react';
import { TopBar } from './TopBar';
export class Layout extends React.Component<any, any> {
...
};
I get the following TypeScript error
(TS) Module: "C:/....../TopBar" has no exported member 'TopBar'.
Prior to adding the React-Redux code the class definition was
export default class TopBar extends React.Component<any, any>{
...
};
and I was able to reference the TopBar component in Layout w/ no errors.
Now that I'm adding the React-Redux connect() statement how do I properly reference TopBar in another component?
It is a default export, you have to import this way.
import * as React from 'react';
import TopBar from './TopBar';
export class Layout extends React.Component<any, any> {
...
};

How to access state of one component into another component in react

import React, { Component } from 'react';
class one extends React.Component
{
constructor()
{
super();
this.state = {
number:26
}
}
render()
{
return(
<div></div>
);
}
}
export default one;
import React, { Component } from 'react';
import one from './one'
class HomePage extends React.Component
{
render()
{
return(
<div>{one.state.number}</div>
);
}
}
export default HomePage;
is it possible to access number state
is there any way to access state of one component into another component?
please suggest me if any solution is present.
As Shubam has explained it, Though I would like to form it as a complete answer
First of all, I would like to let you know that Never Use lowercase letters to name your React Components.So name your component to One instead of one.
Now Comming back to your question:-
No This is not Possible, If your app contains few components then it's better to pass the state object as the props, But if your app contains too many components then better to use predictable state containers like Redux or Flux rather than passing state as props.
So you may apply these changes and I hope You will get What You Desire:-
One Component:-
import React, { Component } from 'react';
import Homepage from './homepage';
class One extends React.Component
{
constructor()
{
super();
this.state = {
number:26
}
}
render()
{
return(
<Homepage data={this.state}/>
);
}
}
export default One;
Homepage Component:-
import React, { Component } from 'react';
class Homepage extends React.Component
{
render()
{
console.log("this is homepage",this.props);
return(
<div>{this.props.data.number}</div>
);
}
}
export default Homepage;
Please Raise Your doubts if any, Or if you find any error in it.

Click event in ReactJS error: imports/ui/ParentComponent.jsx:5:16: Unexpected token (5:16)

I am trying to learn Event in ReactJS.
I created 2 components
ChildComponent is
import React, { Component } from 'react';
// App component - represents the whole app
export default class ChildComponent extends Component {
render() {
return (
<button onClick={this.props.onBannerClick}>Click me!</button>
);
}
}
And ParentComponent is
import React, { Component } from 'react';
import ChildComponent from './ChildComponent.jsx'
// App component - represents the whole app
export default class ParentComponent extends Component {
performMagic: function() {
alert('TAADAH!');
},
render() {
return (
<BannerAd onBannerClick={this.performMagic} />
);
}
}
but I got the error
Errors prevented startup:
While building for web.browser:
imports/ui/ParentComponent.jsx:5:16: Unexpected token (5:16)
Your application has errors. Waiting for file change.
I think the error is from
performMagic: function() {
alert('TAADAH!');
},
But I do know what the error is.
By the way, can anybody recommends me good debug tools for ReactJS?
Because you're using the ES6 syntax you'll have to bind the function to the instance using the following approach.
constructor(props) {
super(props)
this.performMagic = this.performMagic.bind(this)
}
This will allow you to use the this keyword in the onClick call
import React, { Component } from 'react';
import ChildComponent from './ChildComponent.jsx'
// App component - represents the whole app
export default class ParentComponent extends Component {
constructor(props) {
super(props)
this.performMagic = this.performMagic.bind(this)
}
performMagic() {
alert('TAADAH!');
}
render() {
return (
<BannerAd onBannerClick={this.performMagic} />
);
}
}
Need to write:
performMagic () {
alert('TAADAH!');
},
You need to use new sintax for functions, when write class which is new sintax.
EDIT: You can use "React Developer Tools" chrome extension and gaearon "redux-devtools" for development.
You need to use the new ES6 syntax when making your React Component a class. Use
performMagic() {
alert('TAADAH!');
}
make sure you don't put a comma after the function

Resources