javascript - 访问 URL 外的 axios body 参数
问题描述
我有这个 axios 请求:
axios.all([
axios.post(baseURL+'sentiments', {
sentiment_type: 'positive',
filter_tab: type
}),
axios.post(baseURL+'sentiments', {
sentiment_type: 'negative',
filter_tab: type
})
])
.then(axios.spread(result1, result2) => {
console.log(result1),
console.log(result2)
})
.catch(err => {
console.log(err)
})
然后在我的渲染方法中,我想要实现的是将“sentiment_type”值“正”访问到某个变量或其他东西中,以进一步将其用于条件渲染。
if(sentiment_type === positive){
then do this
}
else{
do something else
}
有没有办法做到这一点?请指导。
解决方案
我假设这部分代码是组件的一部分。您可以将这些变量存储在本地存储中。以下是更多信息的链接:状态和生命周期
和一个例子,我希望这会对你有所帮助。
class parentComponent extends React.Component {
state = {
sentiment_types: [];
}
componentDidMount() {
axios.post(baseURL+'sentiments', {
sentiment_type: 'positive',
filter_tab: type
})
.then((firstResponse) => {
axios.post(baseURL+'sentiments', {
sentiment_type: 'negative',
filter_tab: type
})
.then((secondResponse) => {
// Here you can store you results into the state
this.setState({
sentiment_types: ['positive', 'negative'];
})
})
.catch(err => {
console.log(err);
})
console.log(result)
})
.catch(err => {
console.log(err)
})
}
render() {
<childComponent sentiment_types={this.state.sentiment_types} />
}
}
const childComponent = (props) => {
if(props.state.sentiment_types.includes('positive')){
return <h1>oke</h1>
}
}
推荐阅读
- java - inputstream restful api返回null
- wordpress - 古腾堡自定义阻止 php 渲染问题
- c - 将值从一个函数传递到C中的另一个函数
- performance - 如何读取excel文件并将其用作jmeter中不同主体的变量
- c# - 使用受约束的随机值自定义 AutoFixture 属性生成
- qt - QMAKE_MSC_VER 未设置
- python - Tkinter columnconfigure 权重未调整
- firebase - 为什么 Firestore 文档的大小限制为 1MiB
- scala - Kamon 没有向 prometheus 报告数据
- javascript - 多个按钮未从不同的数组中选择