import axios from 'axios';
axios.get('/api/test/:id')
.then((response) => console.log(response));
.get('/api/test/:id', async(ctx) => {
let theme = await Test.theme(ctx.params.id);
let test = await Test.test(ctx.params.id);
let answearArr = await Promise.all(test.map(item => {
return Test.answear(item.id);
}));
for(let n=0; n<answearArr.length; n++){
test[n].answer = answearArr[n];
}
theme.test = rand_array(0,2,test);
ctx.body = theme;
})
const id = 1
axios.get(`/api/test/${id}`).then((response) => console.log(response));
router.get('/testing/:id', async ctx => {
await ctx.render('test', {
title: 'Тест',
})
})
import axios from 'axios';
axios.get('/api/test/:id')
.then((response) => console.log(response));
router.get('/api/test/:id', async(ctx) => {
let theme = await Test.theme(ctx.params.id);
let test = await Test.test(ctx.params.id);
let answearArr = await Promise.all(test.map(item => {
return Test.answear(item.id);
}));
for(let n=0; n<answearArr.length; n++){
test[n].answer = answearArr[n];
}
theme.test = rand_array(0,2,test);
ctx.body = theme;
})
<TestItem id={3}/>
const TestItem = ({id})=><div onClick={()=>requestHandler(id)}</div>
<div class='container'>
<div id='root'></div>
</div>
<script src='/client/testing.js'></script>
componentWillMount() {
axios.get(`http://exmaple.com/blog/${this.props.id}`)
.then(response => this.setState(response.data))
}
const Router = require('koa-router'),
User = require('./model/user'),
Test = require('./model/test'),
router = new Router();
router.get('/testing/:id', async ctx => {
await ctx.render('test', {
title: 'Текст',
path: 'test'
})
})
router.get('/api/test/:id', async(ctx) => {
let theme = await Test.theme(ctx.params.id);
let test = await Test.test(ctx.params.id);
let answearArr = await Promise.all(test.map(item => {
return Test.answear(item.id);
}));
for(let n=0; n<answearArr.length; n++){
test[n].answer = answearArr[n];
}
theme.test = rand_array(0,2,test);
ctx.body = theme;
})
include layout
.container
script(src='/client/testing.js'
import axios from 'axios';
axios.get('/api/test/1')
.then((response) => console.log(response));
import axios from 'axios';
console.log(this.props.id);
axios.get('/api/test/1')
.then((response) => console.log(response));
Uncaught TypeError: Cannot read property 'props' of undefined
class Form extends React.Component{
constructor(props){
super(props);
this.state = {
currentQuestion: 0
}
}
componentWillMount = () => {
console.log(this.props.id);
axios.get('/api/test/{this.props.id}')
.then((response) => console.log(response));
};
onClick = (e) => {
if(this.state.currentQuestion != 2){
e.preventDefault();
}
this.setState({
currentQuestion: ++this.state.currentQuestion
})
}
render(){
const { thema } = this.props;
const question = thema[0].test[this.state.currentQuestion];
return(
<div>{this.componentWillMount}</div>
)
}
}
render() {
return (
<div>
<BlogPost id={123} /> //Сюда он передал id
</div>
);
}
// BlogPost.js
componentWillMount() {
axios.get(`http://exmaple.com/blog/${this.props.id}`) // А тут он вывел
.then(response => this.setState(response.data))
}
<List>
- запрашивает с апи список текущих ид и внутри себя делает рендер типо как-то так<Item>
запрашивате данные для себяrender(){
return <div>{this.props.ids.map(id=><Item id={id}>)}<div>
}