I have a strange problem. When I try to render a component ("parent"), it works fine. but, when I add a child component, I cant see both components on screen (parent&child). My code looks like :
Does not Work:
import React from 'react';
import SonComp from './SonComp';
export default class ParentBox extends React.Component {
render() {
return(
<div>
<div>
<SonComp />
</div>
</div>
);
}
}
Does Work:
import React from 'react';
export default class ParentBox extends React.Component {
render() {
return(
<div>
<div>
<p>Hello<p>
</div>
</div>
);
}
}
The child :
import React from 'react';
export default class SonComp extends React.component {
render() {
return (
<div>
<p>hi</p>
</div>
);
}
}
Change the React.component in your does not work code to React.Component.
Related
I need a little help. I'm working on a project that uses Class Components in React and I got stuck with a issue.
How can I pass datas using props?
For example, imagine that I have one Component that have an array in the state:
import React,{Component} from "react";
class CarList extends Component{
constructor(props){
super(props);
this.state = {
carList: ['Jeep', 'Kwid','HB20','Ônix', 'Prisma', 'Gol quadrado']
}
}
render(){
return(
<div>
</div>
);
}
}
export default CarList;
And now I have to call this array in a Option Tag inside a Select Tag.
Let's imagine this component Bellow:
import React from "react";
import { Component } from "react";
import CarList from "./components/Datas";
class App extends Component{
render(){
return(
<div>
<p>I got it! Here is the car list:</p>
<select>
{this.state.CarList.map( (item,x)=>{
return(
<option key={x}>{item}</option>
)
})}
</select>
</div>
)
}
}
export default App;
This piece of code does not work.
the console.log says: "Uncaught TypeError: this.state is null"
I know that I could create a div with my datas and call with , but I have to use props to pass the datas between the Components.
How can I create a callback function using props to resolve this?
Hi!
I tried to call using this.state, but I got "this.state is not defined"
To pass your data as a props you have to pass it to your child component like this from your parent component :
class ParentComponent extends React.Component {
state = {
carList: [],
};
constructor(props) {
super(props);
this.state = {
carList: ['Jeep', 'Kwid', 'HB20', 'Ônix', 'Prisma', 'Gol quadrado'],
};
}
render() {;
return (
<div>
<ChildComponent carList={this.state.carList || []} />
</div>
);
}
}
and then it is accessible in your child component with this.props.
you can use this props in child component like this:
class ChildComponent extends React.Component {
render() {
return (
<div>
{this.props.carList.map((cars, index) => {
return (
<span key={index}>
{cars}
<br />
</span>
);
})}
</div>
);
}
}
Edit -
if you want to see source code : https://stackblitz.com/edit/react-ts-ddcylu?file=Parent.tsx
I have a question that I believe that is simple, buuut I couldn't solve it and I didn't find something similar on the internet.
I have a React Component that have an array like state and I'm trying to take this array and put in another array into a select-option.
This is the Component with the array:
import React,{Component} from "react";
class CarList extends Component{
constructor(props){
super(props);
this.state = {
carList: ['Jeep', 'Kwid','HB20','Ônix', 'Prisma', 'Gol quadrado']
}
}
render(){
return(
<div>
<select>
{this.state.carList.map( (item,ii)=>{
return(
<option key={ii}>{item}</option>
)
} )}
</select>
</div>
);
}
}
export default CarList;
And this is the Component that is render on React-dom:
import React from "react";
import { Component } from "react";
import CarList from "./components/Tabela";
class App extends Component{
constructor(props){
super(props);
this.state={
}
}
render(){
return(
<div>
<p>I got it! Here is the car list:</p>
<select>
{this.state.carList.map( (item,x)=>{
return(
<option key={x}>{item}</option>
)
})}
</select>
</div>
)
}
}
export default App;```
Hey!
I tried to call with a map();
I tried to import the component;
I tried to call the array just before import the component;
Not sure if I understand you correctly. Here is an option:
From checking your App component code, the select component would give the same result as this:
render() {
return (
<div>
<p>I got it! Here is the car list:</p>
<CarList />
</div>
);
}
I have a component that I want to show in another site.
components/Hello.js
import React from 'react';
export default class Hello extends React.Component {
render() {
return (
<div><p>Hello</p></div>
)
};
};
Now I want to import it into my site named "Profile.js".
pages/Profile.js
import React from 'react';
import Hello from '../components/Hello';
export default function Profile() {
return(
<div>
{/* Say hello*/}
{Hello}
</div>
);
}
What am I doing wrong? Why wont it say hello on my "Profile.js" page?
React syntax for rendering components is as follow :
export default function Profile() {
return(
<div>
{/* Say hello*/}
<Hello/>
</div>
);
}
I am learning React
While working on Props, I have created a component and using that component in my index.jsx. But the values passed through props are not displayed on the UI.
usingprops.jsx
import React from 'react';
class UsingProps extends React.Component {
render() {
return (
<div>
<p>{this.props.headerProp}</p>
<p>{this.props.contentProp}</p>
</div>
);
}
}
export default UsingProps;
index.jsx
import React from 'react';
import UsingProps from './Props/UsingProps.jsx';
class App extends React.Component {
render() {
return (
<div>
<UsingProps />
</div>
);
}
}
const myElement = <App headerProp="Header from props!!!" contentProp="Content from props!!!" />;
ReactDOM.render(myElement, document.getElementById('root'));
export default App;
You are putting the headerProp on the App component, not the UsingProps component, which is where you are trying to access it. You need to revise it to this:
class App extends React.Component {
render() {
return (
<div>
<UsingProps headerProp="Header from props!!!" contentProp="Content from props!!!" />
</div>
);
}
}
ReactDOM.render(<App />, document.getElementById('root'));
I am passing two functions from parent to child component, one function is working properly, but another is throughs error
this.props.getAddNewObj is not a function
Please any one help me
import ChildComModal from './modal';
class ParentCom extends Component{
handleModal=()=>{
this.refs.modalRef.handleModal()
}
getAddNewObj=(event)=>{
this.setState({newObj:event})
}
getNewEditedObj=(event)=>{
this.setState({newObj:event})
}
render(){
return(
<React.Fragment>
<ChildComModal
ref="modalRef"
getAddNewObj={this.getAddNewObj}
getNewEditedObj={this.getNewEditedObj}
/>
<Button onClick={this.handleModal}>Open Modal</Button>
</React.Fragment>
)
}
}
export default ParentCom ;
class ChildCom extends Component{
handleModal=()=>{
this.setState({isOpen:true})
}
habdleAdd=(e)=>{
this.props.getAddNewObj(e)
}
handleEdit=(e)=>{
this.props.getNewEditedObj(e)
}
}
export default ChildCom;