ReactJs – 从调用中解析JSON

huangapple go评论67阅读模式
英文:

ReactJs - Parsing JSON from call

问题

以下是您要翻译的内容:

I have a React form with multiple fields that need to be pre-filled with an async json call. I'm trying to set the value of the buttons with attributes from the main json but the data isn't available after load.

How would you access generic json values from an async call on load?

export function App() {
    const [values, setValues] = useState([]);
    
    const [valueOne, setValueOne] = useState();
    const [valueTwo, setValueTwo] = useState();
    
    
    // gets called once, no dependencies, loads the grid data
    useEffect(() => {
        //returns
        //Object { valueOne: "N", valueTwo: "Y" }
        Service.getValues()
            .then(data => setValues(data)
            );
    
        //these are not setting 
        setValueOne(values.valueOne)
        setValueTwo(values.valueTwo)
    }, []);
    
    return (
        <>
            <ToggleButtonGroup size="sm" type="radio" name="testOne"
                                               value={valueOne}>
                                <ToggleButton id="tbg-radio-1" value={'Y'} variant={'outline-success'}>
                                    Running
                                </ToggleButton>
                                <ToggleButton id="tbg-radio-2" value={'N'} variant={'outline-danger'}>
                                    Suspended
                                </ToggleButton>
                            </ToggleButtonGroup>
    						
                            <ToggleButtonGroup size="sm" type="radio" name="testTwo"
    											value={valueTwo}>
                                <ToggleButton id="tbg-radio-3" value={'Y'} variant={'outline-success'}>
                                    Running
                                </ToggleButton>
                                <ToggleButton id="tbg-radio-4" value={'N'} variant={'outline-danger'}>
                                    Suspended
                                </ToggleButton>
                            </ToggleButtonGroup>
        </>
    );
}

export default App;

请注意,我已经删除了代码部分,只提供了您要翻译的注释和文本。

英文:

I have a React form with multiple fields that need to be pre-filled with an async json call. I'm trying to set the value of the buttons with attributes from the main json but the data isn't available after load.

How would you access generic json values from an async call on load?

export function App() {
const [values, setValues] = useState&lt;any[]&gt;([]);
const [valueOne, setValueOne] = useState();
const [valueTwo, setValueTwo] = useState();
// gets called once, no dependencies, loads the grid data
useEffect(() =&gt; {
//returns
//Object { valueOne: &quot;N&quot;, valueTwo: &quot;Y&quot; }
Service.getValues()
.then(data =&gt; setValues(data)
);
//these are not setting 
setValueOne(values.valueOne)
setValueTwo(values.valueTwo)
}, []);
return (
&lt;&gt;
&lt;ToggleButtonGroup size=&quot;sm&quot; type=&quot;radio&quot; name=&quot;testOne&quot;
value={valueOne}&gt;
&lt;ToggleButton id=&quot;tbg-radio-1&quot; value={&#39;Y&#39;} variant={&#39;outline-success&#39;}&gt;
Running
&lt;/ToggleButton&gt;
&lt;ToggleButton id=&quot;tbg-radio-2&quot; value={&#39;N&#39;} variant={&#39;outline-danger&#39;}&gt;
Suspended
&lt;/ToggleButton&gt;
&lt;/ToggleButtonGroup&gt;
&lt;ToggleButtonGroup size=&quot;sm&quot; type=&quot;radio&quot; name=&quot;testTwo&quot;
value={valueTwo}&gt;
&lt;ToggleButton id=&quot;tbg-radio-3&quot; value={&#39;Y&#39;} variant={&#39;outline-success&#39;}&gt;
Running
&lt;/ToggleButton&gt;
&lt;ToggleButton id=&quot;tbg-radio-4&quot; value={&#39;N&#39;} variant={&#39;outline-danger&#39;}&gt;
Suspended
&lt;/ToggleButton&gt;
&lt;/ToggleButtonGroup&gt;
&lt;/&gt;
);
}
export default App;

答案1

得分: 1

setState 在下一次渲染之前不会设置状态,因此当你拥有 data 时,需要直接从中获取 valueOnevalueTwo,而不是调用 setState 然后立即尝试访问它。

Service.getValues()
        .then(data => {
            setValues(data)
            setValueOne(data.valueOne)
            setValueTwo(data.valueTwo)
        });

或者,可以添加另一个 useEffect

useEffect(() => {
    // 返回
    // Object { valueOne: "N", valueTwo: "Y" }
    Service.getValues()
        .then(data => setValues(data));
}, []);

useEffect(() => {
    if (values.valueOne) {
        setValueOne(values.valueOne)
        setValueTwo(values.valueTwo)
    }
}, [values]);
英文:

setState doesn't set state until the next render, so you need to get valueOne and valueTwo from data directly when you have it, not call setState and then immediately try to access it.

Service.getValues()
.then(data =&gt; {
setValues(data)
setValueOne(data.valueOne)
setValueTwo(data.valueTwo)
});

Or, add another useEffect:

useEffect(() =&gt; {
//returns
//Object { valueOne: &quot;N&quot;, valueTwo: &quot;Y&quot; }
Service.getValues()
.then(data =&gt; setValues(data)
);
}, []);
useEffect(() =&gt; {
if (values.valueOne) {
setValueOne(values.valueOne)
setValueTwo(values.valueTwo)
}
}, [values])

huangapple
  • 本文由 发表于 2023年4月20日 00:28:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76056851.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定