사용자가 항목을 클릭하면 Ajax 요청이 서버로 전송되고 수신 된 데이터가 반응 구성 요소에 표시되어야하는 콘텐츠 링크 목록이 있습니다. componentWillReceiveProps
을 사용하여 요청을 보내지 만 URL을 새로 고치지 않으면 라우터가 구성 요소를 호출하지 않습니다. 이 App.js 파일의 "렌더링"기능의반응 구성 요소 렌더링에서 ajax 사용
render((
<Router history={hashHistory}>
<Route path="/" component={App}>
<Route path="/content/:id" component={UmContent} />
</Route>
</Router>
), document.getElementById('root'));
:
이하는 index.js 파일의
render() {
return (
<div>
<h1>App</h1>
<ul>{this.renderItems()}</ul>
{this.props.children}
</div>
);
}
그것은 UmContent.js 파일입니다 :
export default class UmContent extends Component {
state = {
data: ''
}
componentWillReceiveProps(props) {
if (!props || !props.params || !props.params.id) {
return;
}
console.log(props.params.id);
let itemId = props.params.id;
let that = this;
$.ajax({
url: "http://192.168.78.2:8585/Default/mygetcontenttoactionresult/" + itemId,
type: 'GET',
dataType: 'html'
}).done((data)=> {
that.setState({data: data});
}).fail(()=> {
that.setState({data: <div> Something is wrong</div>});
});
}
render() {
return (
<div ref="aaa">
<UmC content={this.state.data}/>
</div>);
}
}
'componentWillReceiveProps'는 컴포넌트가 실제로 새로운 소품을 얻었을 때만 발생합니다. 이런 일이 어디 있니? –
@MichaelParker 실제로 내 구성 요소는 자체를 업데이트하기 위해 url 매개 변수를 사용해야하므로 props.params가 변경되었다고 생각합니다. – sahar
나는 당신이 내가 무엇을 말하려고하는지 이해하지 못한다고 생각합니다. 'componentWillReceiveProps'는 [React lifecycle] (https://facebook.github.io/react/docs/component-specs.html#updating-componentwillreceiveprops)의 일부이며, 컴포넌트가 마운트되었을 때 실행되지 않습니다. * after * 그것이 마운트되고 구성 요소가 새로운 소품을 얻습니다. 아약스 요청이 실제로 발사되고 있습니까? 그렇다면 구성 요소가 수신하기 위해 소품은 무엇입니까? –