简体   繁体   English

如何在 integer 数据类型的 React 组件中初始化 state?

[英]How can I initialize state inside a React Component for an integer data type?

I'm trying to initialize state inside one of my React Components for an integer data type.我正在尝试在我的一个 React 组件中为 integer 数据类型初始化 state 。 Right now my state looks like this:现在我的 state 看起来像这样:

state = {
  username: '',
  email: '',
  password: '',
  user_id: null,
  id: null,
  toy_id: null,
  loading: false,
}

Because they are not strings I have been using null for user_id , id and toy_id , however later down the road when I pass down the properties:因为它们不是字符串,所以我一直将null用于user_ididtoy_id ,但是后来当我传递属性时:

const { email, password, user_id, id, toy_id } = this.state

// ...

await this.props.loginUserToy(email, password, id, toy_id)

console.log('LOGIN loginUserToy email', email)
console.log('LOGIN loginUserToy password', password)
console.log('LOGIN loginUserToy id', id)
console.log('LOGIN loginUserToy toy_id', toy_id)

Here is loginUserToy from my action creators page:这是我的动作创建者页面中的loginUserToy

export const loginUserToy = async (email, password, id, toy_id) => {
  try {
    const response = await axios({
      method: 'POST',
      url: `${API_URL}/api/get_token`,
      data: {
        email,
        password
      }
    })

    const { token } = response.data

    const userResponse = await axios({
      method: 'POST',
      url: `${API_URL}/api/login`,
      headers: {
        Authorization: `${token}`
      },
      data: {
        email,
        password
      }
    })

    const getProfileResponse = await axios({
      method: 'POST',
      //url: `${API_URL}/api/user_profile`,
      url: `${API_URL}/api/toy`,
      headers: {
        Authorization: `${token}`
      },
      data: {
        id,
        toy_id
      }
    })

    const { Name, Description, Where, When, Picture } = getProfileResponse.data

    return {
      type: FETCH_TOY,
      payload: {
        token,
        email,
        password,
        id,
        toy_id,
        name: Name,
        description: Description,
        location: Where,
        date: When,
        image: Picture
      }
    }
  }
  catch (err) {
    console.log('Exception in actions/user/loginUser err', err)
  }
}

I get back 't' & 't' for the first two logs (which is correct) but I continue to get back null for the second two logs.我为前两个日志返回“t”和“t”(这是正确的),但我继续为后两个日志返回null I understand an empty string is different from a null which means a non-existent value.我了解空字符串与null不同,这意味着不存在的值。

However, is there something else I can do here?但是,我还能在这里做些什么吗?

Try simply initializing them as 0 instead of as null .尝试简单地将它们初始化为0而不是null

state = {
  username: '',
  email: '',
  password: '',
  user_id: 0
  id: 0,
  toy_id: 0,
  loading: false,
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在创建时为 React 组件初始化 Redux state? - How can I initialize Redux state for React component on its creation? 如何在反应中从父组件内部初始化子组件 state? - How can you initialize a child components state from inside a parent component in react? 如何在反应 class 组件中设置另一个 state 以将数据从初始 state 传递到 - How can I set another state to pass data to from the initial state in a react class component 如何通过在 ReactJS 中将 props 从父组件传递到子组件来初始化状态? - How can i initialize state by passing props from parent component to child component in ReactJS? 我应该在React组件中初始化一个null状态变量吗? - Should I initialize a null state variable in a React component? 如何解构此反应组件中的 state 对象数组以使用单个字段数据 - How can I destructure a state array of objects in this react component to use individual field data 尝试在变量中使用上述数据时,如何在组件状态无效的内部使用json数据? - How can I use json Data inside of my component state invalid when trying to use said data inside a variable? 无法使用 state 中的数组初始化 React 复选框 - Can't initialize React checkboxes with an array inside state 如何在React中设置视频端组件的状态? - How can I set the state of a component on video end in react? 我如何使用ES6在React组件中保持状态 - How can i keep state in a React component using ES6
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM