如何在另一个组件中获取Redux Form数据

发布于 2021-01-31 23:00:24

如何从Redux表单传递数据,以便可以在App.js中访问该数据?这是我使用redux-
form编写的代码。单击提交后,数据应该从form.js传递到app.js. 并且数据应显示在页面上。

这是form.js-

const Form=({fields:{name,address}})=>(
  <form>
    <center>
    <div>
      <label>First Name</label>
      <Field type="text" component="input" placeholder="Name" name="name"/>
    </div>
    <div>
      <label>Address</label>
      <Field type="text" component="input" placeholder="Phone" name="phone" />
    </div>
    <button type="submit">Submit</button>
  </center>
  </form>
)


export default reduxForm({
  form: 'form',
  fields: ['name', 'address']
})(Form);

如何将输入的数据传递给app.js?

关注者
0
被浏览
84
1 个回答
  • 面试哥
    面试哥 2021-01-31
    为面试而生,有面试问题,就找面试哥。

    您需要做的就是getFormValues获取redux field

    因此,在App.js中,您可以拥有

    import {getFormValues} from 'redux-form';
    
    ..
    const App = (props) => {
          var {name, phone} = props.formStates
          console.log(name, phone);
    }
    
    function mapStateToProps(state) {
        return {
             formStates: getFormValues('form')(state) // here 'form' is the name you have given your redux form 
        }
    }
    
    export default connect(mapStateToProps)(App)
    


知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看