2016-09-08 26 views
1

我只是一个学习React/Redux的新孩子,它非常酷。但是一些函数不支持es6标准。是的,我看到了很多替代方法但我无法获得它(使用高阶函数)。任何帮助对混入添加到我的阵营ES6代码在es6标准中使用mixin的最佳方式是什么?

import React from 'react'; 
    import Fetch from 'whatwg-fetch'; 
    import jwt from 'jwt-simple'; 
    import Reflux from 'reflux'; 

    const secret = 'goal'; 

    class Form extends React.Component{ 
     // mixins:[ 
     //  Not supported in es6 
     // ], 
     constructor(props){ 
      super(props); 
      this.state = { 
       value:'', 
       posted:'' 
      } 
     } 

     onPost(e){ 
      e.preventDefault(); 
      if(this.refs.email.value && this.refs.name.value != ''){ 
       const data = { 
        email:this.refs.email.value, 
        name:this.refs.name.value 
       }; 
       const encodehead = jwt.encode(data,secret); 
       const encrypt = { 
        encode:encodehead 
       }; 

       $.ajax({ 
        type: 'POST', 
        url: '/post', 
        headers: { 
         kovam: encodehead 

        }, 

       }) 

      } 
     } 
     render(){ 
     console.log(this.state); 
      return (
       <div> 
        <form className="postform" onSubmit={this.onPost.bind(this)}> 
         <label>Email Addres</label> 
         <input type="email" ref="email" className="form-control"/> 
         <label>Name</label> 
         <input type="text" ref="name" className="form-control"/> 
         <br/> 
         <br/> 
         <button type="submit" className="btn btn-primary">Post To server</button> 
        </form> 

       </div> 
      ) 
     } 
    } 

    export default Form; 

回答

1

您应该使用Higher Order Components而是混入不再在ES6类中使用,请参阅this article

+0

你可以为这个代码写一个高阶函数的例子吗 – Nane

+0

你自己试一试并发布 - 这就是你学习的方式。那我可以检查一下。 – lustoykov

+0

好吧,我会试试@leo – Nane

相关问题