我如何通过发布请求传递整个反应状态

时间:2018-10-28 14:55:29

标签: ajax reactjs axios

我正在考虑使用axios通过发帖请求传递整个反应状态

所以,这就是我所做的

import React from 'react';
import { MaterialCommunityIcons as Icon } from '@expo/vector-icons';
import { IconProps } from '@types/react-native-vector-icons/Icon'
import glyphmap from 'react-native-vector-icons/glyphmaps/MaterialCommunityIcons.json'

type MyMap<T> = {
    [ P in keyof T ]: number
};

// spread keys after converting glyphmap into MyMap
type MapKeys = keyof MyMap<typeof glyphmap>

interface Props extends IconProps {
    name: MapKeys
}

const MyIcon = (props: Props) => (
    <Icon {...props} />
)

const Test = props => (
    <MyIcon name="account" />
)

但是可能这不是您通过发帖请求发送整个状态的方式。所以说这是我的发帖请求

 axios.post("/profile", (
          {state: this.state}
        )).then(response => {
          console.log(response)
        }).catch(error => {
          console.log("this is error", error)
        })
        this.props.history.replace('/home');
      }

我想立即发送到我的节点路由,我该怎么办?

Ps:这是我的节点路线

state = {
  bestAchievement: "",
  secondaySports: "",
  dob: "",
  primarySports: "",
  role: "",
  role1: "",
  phone: "",
  userid: "",
  address: "",
  state: "",
  city: "",
  pinCode: "",
  bio: ""
}

1 个答案:

答案 0 :(得分:1)

axios.post的第二个参数是一个键值对象,其中包含您要发送到服务器的数据,因此this.state就可以正常工作。

axios.post("/profile", this.state).then(response => {
  console.log(response);
}).catch(error => {
  console.log("this is error", error);
});