2017-10-17 14 views
1

私は、このコンポーネントを持っている:JSX印刷テキスト

<Button type="submit" { invalid ? 'primary': null }> 

このコンポーネントはコンポーネントスタイル設定さ:

import styled from 'styled-components'; 

export const Button = styled.button` 
    font-size: 15px; 
    padding: 0.25em 1em; 
    border: solid 1px ${(props) => { 
     let color; 
     if (props.primary) { 
     color = 'red'; 
     } else { 
     color = '#ffffff'; 
     } 
     return color; 
    }}; 
    `; 

私はこのエラーを取得する:

Syntax error: Unexpected token ^invalid, expected ... (64:54)

無効になっている場合は、「プライマリ」プロパティを送信して、これを取得する必要があります。

<Button type="submit" primary/> 

私が書き込みをしたくない:

primary = { invalid } 

コンポーネントは、このボタンは呼び出します。

import React from 'react'; 
import { Button } from './layouts/cssstyled'; 
const getConditionalProps = (props) => { 
    // fill your `invalid` variable in this function or pass it to it 
    const myprops = {}; 
    myprops.primary = true ; 
    myprops.secondary = false; 
    return myprops; 
} 

const Form = (props) => { 
    console.log('form props'); 
    console.log(props); 

    const { handleSubmit, invalid, pristine, reset, submitting, t } = props; 
    return (
    <form onSubmit={handleSubmit}> 
     <p>Invalid? {JSON.stringify(invalid)}</p> 

     <Button type="submit" disabled={submitting} primary={invalid ? "" : null} > 
      Buton styled component does not work 
     </Button> 
     <button primary={invalid ? "" : null}> button native works</button> 

     <div className="formError"> 
      {pristine} 
      {invalid && t('form.haserrors')} 
     </div> 

     </div> 

    </form> 
); 
}; 

export default reduxForm({ 
    form: 'CustomerForm', // a unique identifier for this form 
})(Form); 
+1

'primary = {invalid}'と書くのは間違いありません。それを書かない理由がありますか? – ZekeDroid

+0

私は「プライマリ」を必要とするCSSライブラリを使用していますが、jsxでテキストを印刷できない技術的な理由はなぜですか? – DDave

+0

@DDave reactは印刷ライブラリではないため、HTMLにレンダリングされる仮想DOMです。 – Sulthan

答えて

0

あなたの条件は次のようになりますので、あなたは、ブール値を使用することができます。

<Button type="submit" primary={ !!invalid } > 

また、falseの場合にボタンにプライマリ値を設定したくない場合は、これを行うことができます:

const button = invalid ? <Button type="submit" primary > : <Button type="submit" > 
+1

しかし、

+0

私は自分の答えを更新しました。 – Shota

+0

私は、あなたの例でプライマリが印刷されているか、または可能でない場合にinvalidがfalseであれば 'プライマリ'を表示したくないのですか? – DDave

0

私はReact-JS Spread Attributesを使用することをお勧めします。条件付きの振る舞いを持つコンポーネント上の小道具を広めるための素晴らしいツールです。

class MyComponent extends React.Component { 
    // ... 

    getConditionalProps() { 
     const props = {}; 
     // fill your `invalid` variable in this function or pass it to it 
     if (invalid) { 
      props.primary = true; 
     } 
     return props 
    } 

    render() { 
     return (
      <Button type="submit" 
        { ...this.getConditionalProps() } /> 
     ); 
    } 
} 

別の解決策は、条件付きのレンダリングです:

class MyComponent extends React.Component { 
    // ... 

    render() { 
     return (invalid ? 
      <Button type="submit" primary /> 
      : 
      <Button type="submit" /> 
     ); 
    } 
} 

あなたの問題へのよりよい解決策があるかもしれないことに注意してください:あなたがする必要があります特定の小道具を送信するかどうかを親コンポーネントから決定しようとするのではなく、コンポーネント自体の内部からButtonコンポーネントの動作を処理します。しかし、上記のソリューションはあなたが必要としているものとまったく同じです。

+0

ありがとう、私はこれを得ることができます

+0

私は 'primary' propの中に' true'を入れる答えを更新しました。これは '

+0

ありがとうございますが、あなたのソリューションはboolenの値では動作せず、プロパティーが表示されません。ちょうどその小道具が文字列の値を持っている場合 – DDave

関連する問題