Reactjs 使用React语义UI中的服务器响应数据更新模式值
我有一个多个带有一些ID的超链接。单击链接时,我必须在模式中显示加载符号,并且我必须进行axios调用。一旦api返回数据,我就必须以相同的模式更新结果 下面是我的代码Reactjs 使用React语义UI中的服务器响应数据更新模式值,reactjs,modal-dialog,state,semantic-ui,prop,Reactjs,Modal Dialog,State,Semantic Ui,Prop,我有一个多个带有一些ID的超链接。单击链接时,我必须在模式中显示加载符号,并且我必须进行axios调用。一旦api返回数据,我就必须以相同的模式更新结果 下面是我的代码 class App extends React.Component { renderContent() { return ( // This data generated using map function <Modal trigger={<a onClick={() =
class App extends React.Component {
renderContent() {
return (
// This data generated using map function
<Modal trigger={<a onClick={() => this.generateAppJson("1") }>Generate App JSON</a> } basic size='small'>
<Header icon='spinner loading' content='Get the name' />
<Modal.Content>
<p>Loading...</p> {/* Here I have to update the content that received form generateAppJson() function*/}
</Modal.Content>
</Modal>
<Modal trigger={<a onClick={() => this.generateAppJson("2") }>Generate App JSON</a> } basic size='small'>
<Header icon='spinner loading' content='Get the name' />
<Modal.Content>
<p>Loading...</p> {/* Here I have to update the content that received form generateAppJson() function*/}
</Modal.Content>
</Modal>
)
}
generateAppJson(id) {
console.log(id)
// Here I'll make axios call and I have to update the result in the same model
}
render() {
return (
<Segment>
{this.renderContent()}
</Segment>
);
}
}
类应用程序扩展了React.Component{
renderContent(){
返回(
//此数据是使用map函数生成的
您需要将axios返回数据保存到组件状态,该状态将自动重新启动您的应用程序
因此,首先需要在axios调用返回时更新组件状态:
async generateAppJson(id) {
const results = await axios.post(someurl, id);
// this line will cause a rerender
this.setState({results})
}
然后需要初始化组件中的状态:
class App extends React.Component {
state = { results: [] }
然后需要检查渲染函数中的状态:
<Modal.Content>
{ !this.state.results.length && <p>Loading...</p>}
{ this.state.results.length &&
this.state.results.map(result => <someothercomponent key={result} /> )
}
</Modal.Content>
{!this.state.results.length&正在加载…}
{this.state.results.length&&
this.state.results.map(result=>)
}
您需要将axios返回数据保存到组件状态,该状态将自动重新启动您的应用程序
因此,首先需要在axios调用返回时更新组件状态:
async generateAppJson(id) {
const results = await axios.post(someurl, id);
// this line will cause a rerender
this.setState({results})
}
然后需要初始化组件中的状态:
class App extends React.Component {
state = { results: [] }
然后需要检查渲染函数中的状态:
<Modal.Content>
{ !this.state.results.length && <p>Loading...</p>}
{ this.state.results.length &&
this.state.results.map(result => <someothercomponent key={result} /> )
}
</Modal.Content>
{!this.state.results.length&正在加载…}
{this.state.results.length&&
this.state.results.map(result=>)
}