0

I'm new to react to anyone can help me out to make sure to define a component inside another component in react

1
  • 1
    Not sure what you mean. Happy to undo the downvote if you can clarify. For instance, do you just want to write two components in the same file? Do you want to write a functional component into the render function of another? What conditions would count as success? Commented Jun 3, 2019 at 18:55

6 Answers 6

3
const ComponentTwo =  ()=> <div>I'm a second component.</div>; 

const ComponentOne =  ({children})=> <div>{children}</div>;   


const App = () => (
       <ComponentOne>
          <ComponentTwo />
       </ComponentOne>
      );

https://reactjs.org/docs/jsx-in-depth.html#children-in-jsx

Sign up to request clarification or add additional context in comments.

Comments

0
import React,{Component} from 'react';
import axios from 'axios/index'

const Course = (props)=>(

<tr>
    <td>{props.course.Name}</td>
    <td>{props.course.Course}</td>
    <td>{props.course.PassMark}</td>
    <td>{props.course.LecturerInCharge}</td>
    <td><button className="btn btn-primary" onClick
{()=>CourseView.buttonClick(props.course.Subjects)}>Subject</button></td>
<td><button className="btn btn-primary" onClick=.{()=>CourseView.TotalCalculation(props.course._id)}>Calculate</button></td>
</tr>
);

1 Comment

componentDidMount() { axios.get('localhost:4000/todos/dropDownData').then(res=>{ let TeamsFromApi = res.data.map(team=>{return{value:team,display:team}}) this.setState( { teams:[{value:'',display:'(select your favourite team)'}].concat(TeamsFromApi) } ); }).catch(err=>console.log(err)); /* axios.get('localhost:4000/sliit/subjects').then(res=>{ this.setState({subjects:res.data}).catch(err=>console.log(err)); })*/ }
0
const Todo = props => (
  <tr>
    <td>{props.todo.todo_description}</td>
    <td>{props.todo.todo_responsible}</td>
    <td>{props.todo.todo_priority}</td>
    <td>
        <Link to={"/edit/"+props.todo._id}></Link>
    </td>
    <td>
        <Link to={"/delete/"+props.todo._id}></Link>
    </td>
</tr>
)

Comments

0
export default class CourseView extends Component{

constructor(props){
    super(props);
    this.state = {
        Courses:[]
    }
}

componentWillMount() {
    axios.get('http://127.0.0.1:4000/course').then((res)=>{
        this.setState({Courses:res.data});
    })
}

listelements(){
    return this.state.Courses.map((course,i)=>{
        return <Course course = {course} key={i}/>
    })
}

render() {
    return(
        <div>
            <table border="2px" align="center" style={{marginTop:100}}>
                <tr>
                <th>Name</th>
                <th>Course</th>
                <th>PassMark</th>
                <th>Lecturer in charge</th>
                <th>Subjects</th>
                    <th>Total</th>
                </tr>
                <tbody>
                {this.listelements()}
                </tbody>
            </table>
        </div>
    )
}

static TotalCalculation(id){
    axios.get('http://127.0.0.1:8080/total/'+id).then((res)=>{
        let total = (res.data.total);
        let subCount = (res.data.subjects.length);
        alert('You have to complete '+subCount+" subjects, Which will be Rs."+total+"/= in total.");
    })
}

static buttonClick(Subjects) {
    let data="";
    console.log(Subjects);
    for(let i=0;i<Subjects.length;i++){
        axios.get('http://127.0.0.1:4000/subject/'+Subjects[i]).then((res)=>{
            data = "Name: "+res.data.Name+", Description: "+res.data.Description+", Amount: "+res.data.Amount;
            data = data+'\n';
            alert(data);
        });

    }

}

}

Comments

0

DBSchema.js

const mongoose = require('mongoose');

const CourseSchema = new mongoose.Schema({
Name:{
    type:String,
    required:true
},
Course:{
    type:String,
    required:true
},
PassMark:{
    type:Number,
    required:true
},
LecturerInCharge:{
    type:String,
    required:true
},
Subjects:{
    type:[mongoose.Schema.Types.ObjectId],

}
});

const SubjectSchema = new mongoose.Schema({
Name:{
    type:String,
    required:true
},
Description:{
    type:String,
    required:true
},
Amount:{
    type:Number,
    required:true
}
});

mongoose.model('Course',CourseSchema);
mongoose.model('Subject',SubjectSchema);

mongoose.connect('mongodb://127.0.0.1:27017/sliit-web',     {useNewUrlParser:true}).then(()=>{
console.log('Connected to DB');
}).catch((err)=>{
   console.error(err);
 });

module.exports = mongoose;

var Express     = require('Express');
var bodyParser  = require('body-parser');
var Cors        = require('cors');
var router      = Express.Router();
var UserRoute   = require('./User/user.route');

var app = Express();

app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(Cors());
app.use('/', UserRoute);

app.listen('8081', '127.0.0.1', function(err) {
if(err) {
    console.log(err);
    process.exit(-1);
}
console.log("Server listen on port 8081");
})

1 Comment

The community encourages adding explanations alongisde code, rather than purely code-based answers (see here).
-1
todoList()
{
    return this.state.todos.map(function(currentTodo,i){
        return <Todo todo={currentTodo} key={i}/>;
    });
}
render()
{
    return(
        <div>
           <h3>Todo List</h3>
           <table>
               <thead>
                   <tr>
                       <th>Description</th>
                       <th>Responsible</th>
                       <th>Prioroty</th>
                       <th>Completed</th>
                   </tr>
               </thead>
               <tbody>
                   {this.todoList}
               </tbody>
           </table>
        </div>
    )
}

1 Comment

Sebastian Eschweiler check this ones github profile for mern tutorials

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.