私はコンテンツリンクのリストを持っています。ユーザーがアイテムをクリックすると、ajaxリクエストがサーバーに送信され、受信データが反応コンポーネントに表示されます。 componentWillReceiveProps
を使用してリクエストを送信しましたが、URLをリフレッシュできません(ルータはコンポーネントを呼び出さない)。ajaxを反応コンポーネントレンダリングで使用する
それはindex.jsファイルです:それはApp.jsファイルの "レンダリング" 機能だ
render((
<Router history={hashHistory}>
<Route path="/" component={App}>
<Route path="/content/:id" component={UmContent} />
</Route>
</Router>
), document.getElementById('root'));
:
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 *それはマウントされ、コンポーネントは新しい小道具を取得します。あなたのAjaxリクエストは実際に発砲ですか?もしそうなら、あなたのコンポーネントはそれを受け取るためにどのような小道具を受け取りますか? –