Preact和React渲染无状态组件之间的区别

时间:2018-01-31 09:19:06

标签: reactjs preact

我正在使用Preact,因为我有一个大小限制的项目。我创建了两个游乐场,一个是Preact,另一个是React。

只需输入搜索字段,即可注意Preact和React的更新方式。反应在DOM节点上重新渲染,Preact增加了它。

我希望Preact的行为像React一样。请帮忙

这是有问题的代码,游乐场可以看到下面的不同行为

public class SigninActivity  extends AsyncTask<String, Void , String>{
    protected void onPreExecute(){}

    @Override
    protected String doInBackground(String... arg0) {
        try{
            link="https://haripriyag2362.000webhostapp.com/login_php.php";
            String data  = URLEncoder.encode("username", "UTF-8") + "=" +
                    URLEncoder.encode(username, "UTF-8");

            URL url = new URL(link);
            URLConnection conn = url.openConnection();
            conn.setDoOutput(true);
            OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
            wr.write( data );
            wr.flush();
            BufferedReader reader = new BufferedReader(new
                    InputStreamReader(conn.getInputStream()));
            StringBuilder sb = new StringBuilder();
            String line = null;
            while((line = reader.readLine()) != null) {
                sb.append(line);
                break;
            }
            return sb.toString();
        } catch(Exception e){
            return new String("Exception: " + e.getMessage());
        }
    }

    //final context context = this;
    @Override
    protected void onPostExecute(String result){
        try{
            JSONObject jObj = new JSONObject(result);

            if(jObj.getBoolean("status")) {
                 Toast.makeText(emp_home.this,"Invalid Succesful..",Toast.LENGTH_SHORT);
            }
            else{
                Toast.makeText(emp_home.this,"Invalid Login ! Try Again",Toast.LENGTH_SHORT);
            }
        }catch (JSONException ex) {
            ex.printStackTrace();
        }
    }
}

以下是React的游乐场:

https://stackblitz.com/edit/react-yey8js?file=index.js

这是CodePen的游乐场:

https://codepen.io/babakness/pen/oEXjPz?editors=1010

1 个答案:

答案 0 :(得分:0)

我发现了差异,Preact的渲染采用了第三个参数,即要替换而不是渲染的DOM节点。

https://github.com/developit/preact/wiki/Differences-to-React#whats-different