獲取組件中的輸入值反應本機 (Get a value of input in a component react native)


問題描述

獲取組件中的輸入值反應本機 (Get a value of input in a component react native)

我在 react native 以獲取日期組件中的輸入值時遇到問題:

我的文件:

main.js:

const DatePicker = require('./DatePicker');
    render(){
        return (
         <DatePicker defaultDate={defaultDate} minDate="01/01/1970" />
        )
    }

DatePicker .js:

import React, { Component } from 'react'
import DatePicker from 'react‑native‑datepicker'
    class MyDatePicker extends Component {
        constructor(props){
            super(props)
            this.defaultDate = props.defaultDate;
            this.minDateProp = props.minDate;
            this.state = {date:this.defaultDate}
        }

        render(){
            return (
                <DatePicker
                    style={{flex:1, marginLeft: 8}}
                    date={this.state.date}
                    mode="date"
                    format="DD/MM/YYYY"
                    minDate={this.minDateProp}
                    maxDate={this.defaultDate}
                    confirmBtnText="Confirm"
                    cancelBtnText="Cancel"
                    customStyles={{
                        dateText: {
                            color: '#2471ab',
                            fontSize: 15
                        }
                    }}
                    onDateChange={(date) => {this.setState({date: date})}}
                />
            )
        }
    }

現在,當我選擇一個日期時,它會更改文本並且一切正常,但是當我提交表單時如何存儲結果?感謝您的建議。


參考解法

方法 1:

There are many approaches how you can do that. One of them (which I personally prefer) is to change your custom MyDatePicker in a way to make it "presentational" component (read more about it here: https://medium.com/@dan_abramov/smart‑and‑dumb‑components‑7ca2f9a7c7d0#.sphuynwa2), i.e.:

import React, {PropTypes} from 'react';
import DatePicker from 'react‑native‑datepicker';

const customStyles = {
  dateText: {
    color: '#2471ab',
    fontSize: 15
  }
};

const style = {
  flex:1,
  marginLeft: 8
};

function MyDatePicker(props) {
  return (
    <DatePicker
      style={style}
      date={props.date}
      mode="date"
      format="DD/MM/YYYY"
      minDate={props.minDate}
      maxDate={props.defaultDate}
      confirmBtnText="Confirm"
      cancelBtnText="Cancel"
      customStyles={customStyles}
      onDateChange={props.onDataChange} />
  );
}

MyDatePicker.propTypes = {
  minDate: PropTypes.string,
  maxDate: PropTypes.string,
  date: PropTypes.string
};

export default MyDatePicker;

This approach will help you to store your state at the level of "container" component (or redux / mobx / whatever). For example:

import React, {Component} from 'react';
import MyDatePicker from './MyDatePicker';

class FormContainer extends Component {
  constructor(props) {
    super(props);
    const date = Date.now();
    this.state = {
      date: `${date.getDay()}/${date.getMonth()}/${date.getYear()}`
    };

    this.submit = this.submit.bind(this);
  }

  submit() {
    // submit this.state to the server
  }

  render() {
    return (
      <form>
        <MyDatePicker 
          date={this.state.date}
          onDateChange={(date) => this.setState({date})} />
        <button onClick={this.submit}>Submit date</button>
      </form>
    );
  }
}

Of course, it's just a rough implementation, but it illustrates the approach.

方法 2:

You are actually storing it in State :

onDateChange={(date) => {this.setState({date: date})}}

If you are going to navigate to another page, you can pass it through props. assuming you're using react‑native‑router‑flux for navigation :

Actions.componentNameToNavigate({ selectedDate: this.state.date });

If you're not navigating to another page and just want to submit it in same page, do something like this :

import React, {Component} from 'react';
import MyDatePicker from './MyDatePicker';

class FormContainer extends Component {
  constructor(props) {
    super(props);
    const date = Date.now();
    this.state = {
      date: `${date.getDay()}/${date.getMonth()}/${date.getYear()}`
    };

    /* this.submit = this.submit.bind(this); */
    // no need to bind it here if you use
    //this.functionName();
  }

  submit(date) {
    // instead of reading it from state, you can pass the date to submit function. that way you dont even need to store it in state
  }

  render() {
    return (
      <form>
        <MyDatePicker 
          date={this.state.date}
          onDateChange={(date) => {
      this.setState({date});
      this.submit(date);
      /// you can pass date as well as accessing it from state
    }
  } />
        <button onClick={this.submit}>Submit date</button>
      </form>
    );
  }
}

Hope it helps.

(by gregoryAlexey KureevAta Mohammadi)

參考文件

  1. Get a value of input in a component react native (CC BY‑SA 2.5/3.0/4.0)

#store #react-native #datepicker






相關問題

Sencha touch 2:通過代理在商店檢索信息 (Sencha touch 2: retrieve infos by proxy in store)

Đại diện cửa hàng ExtJS Costum (ExtJS Costum store representation)

Cửa hàng Sencha Touch 2 đạt kỷ lục (Sencha Touch 2 store get record)

更換和存放 (Replacing and Storing)

如何重命名Window Store App的.exe(可執行文件)文件? (How to rename .exe (executable) file of Window Store App?)

Socket.io 和 Extjs:為 Store 檢索數據 (Socket.io and Extjs: Retrieving data for Store)

獲取組件中的輸入值反應本機 (Get a value of input in a component react native)

為小組存儲生產密碼的最佳實踐 (Best practices for storing production passwords for small groups)

有人可以解釋一下 iphone 證書的東西,步驟是什麼樣的嗎? (Can someone explain me the iphone certificate stuff, how the steps look like?)

用戶退出應用程序時在 MapKit 中存儲註釋 (Store annotations in MapKit when user quit application)

使用 Injector angular 7 將 Store 注入子類 (Injecting Store to sub classes using Injector angular 7)

通過 VBA 中的輸入創建數組 (Creath an array by input in VBA)







留言討論